代码中的 UIButton [英] UIButton in code

查看:13
本文介绍了代码中的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆