UIButton in code(代码中的 UIButton)
问题描述
我需要向 UIButton
添加手势(移动、旋转、缩放).我在代码中创建了按钮,但我不知道如何使用它.如果触摸按钮,我想移动、缩放或旋转.这是我的代码:
I need to add a gesture (move, rotate, scale) to a UIButton
.
I created the button in code, but I don't know how to use it.
If the button is touched I want to move, scale or rotate.
Here is my code:
-(void)addButton{
UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,480)];
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(self.view.bounds.size.width,1000.0);
buttonImage1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonImage1.frame = CGRectMake(100, 20, 100, 100);
UIImage *btnImage1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:adres1]]];
[buttonImage1 setImage:btnImage1 forState:UIControlStateNormal];
[scrollview addSubview:buttonImage1];
buttonImage2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonImage2.frame = CGRectMake(100, 180, 150, 150);
UIImage *btnImage2 = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:adres2]]];
[buttonImage2 setImage:btnImage2 forState:UIControlStateNormal];
[scrollview addSubview:buttonImage2];
buttonImage3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonImage3.frame = CGRectMake(100, 450, 194, 146);
UIImage *btnImage3 = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:adres3]]];
[buttonImage3 setImage:btnImage3 forState:UIControlStateNormal];
[scrollview addSubview:buttonImage3];
}
-(void) move
{
}
推荐答案
我需要向 UIButton 添加手势(移动、旋转、缩放).
I need to add a gesture (move, rotate, scale) to a UIButton.
我不确定我是否理解您所说的添加手势是什么意思.目前,您的按钮根本无法执行任何操作.
I'm not sure I understand what you mean about adding a gesture. Right now, your buttons aren't set up to do anything at all.
我在代码中创建了按钮,但我不知道如何使用它.
I created the button in code, but I don't know how to use it.
您需要为按钮添加目标和操作.当一个按钮被点击时,它会发送一个 action 消息到另一个对象,它是它的 target.要设置按钮,您需要使用从 UIControl
继承的 -addTarget:action:forControlEvents:
方法添加目标和操作.如果您希望按钮向创建按钮的同一对象发送 move
消息,您需要更改 addButton
方法以包含以下内容:
You need to add a target and action to your button. When a button is tapped, it sends an action message to some other object, which is its target. To set up a button, you need to add a target and action using the -addTarget:action:forControlEvents:
method inherited from UIControl
. If you want the button to send a move
message to the same object that's creating the button, you'd change your addButton
method to include this:
[buttonImage1 addTarget:self action:@selector(move) forControlEvents:UIControlEventTouchUpInside];
这篇关于代码中的 UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:代码中的 UIButton
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01