在代码中的UIButton

我需要添加一个手势(移动,旋转,缩放)到一个UIButton 。 我在代码中创build了button,但是我不知道如何使用它。 如果button被触摸,我想移动,缩放或旋转。 这是我的代码:

 -(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。

我不确定我明白你的意思是添加一个手势。 现在,你的button没有设置做任何事情。

我在代码中创build了button,但是我不知道如何使用它。

你需要添加一个目标和行动到你的button。 当一个button被轻敲时,它会发送一个动作消息给其他对象,这是它的目标 。 要设置一个button,需要使用从UIControlinheritance的-addTarget:action:forControlEvents:方法来添加目标和操作。 如果你想让button发送一个move消息给创buildbutton的同一个对象,你可以改变你的addButton方法来包含这个:

 [buttonImage1 addTarget:self action:@selector(move) forControlEvents:UIControlEventTouchUpInside];