代码中的UIButton [英] UIButton in code
问题描述
我需要添加一个手势(移动,旋转,缩放)到 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.
您需要为按钮添加目标和操作。点击按钮时,它会向其他对象发送操作消息,该对象是目标。要设置按钮,需要使用从 UIControl $ c继承的
-addTarget:action:forControlEvents:
方法添加目标和操作$ C>。如果您希望按钮向正在创建按钮的同一对象发送 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!