缩放,移动,旋转许多UIButton

时间:2014-03-09 20:10:49

标签: ios objective-c uibutton uigesturerecognizer

我是xcode的新手。我试着用我的三个按钮做三件事。我需要同时进行缩放,移动和旋转其中一个触摸按钮。 我可以移动我的按钮,但我不能做任何其他事情。我需要在这里使用uigesturerecognizer,我试过,但我不知道如何

有人可以帮帮我吗? 对不起我的英语不好 请告诉我该怎么做。

这是我的代码:

-(void)addButton
{
    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);

    //image1
    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];  
    [buttonImage1 addTarget:self action:@selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside];
    buttonImage1.userInteractionEnabled = YES;
    buttonImage1.multipleTouchEnabled = YES;
    [scrollview addSubview:buttonImage1];
     //image2   
    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];
    [buttonImage2 addTarget:self action:@selector(wasDragged:withEvent:)  forControlEvents:UIControlEventTouchDragInside];
    buttonImage2.userInteractionEnabled = YES;
    buttonImage2.multipleTouchEnabled = YES;
    [scrollview addSubview:buttonImage2];
    //image3
    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]; 
    [buttonImage3 addTarget:self action:@selector(wasDragged:withEvent:)forControlEvents:UIControlEventTouchDragInside];
    buttonImage3.userInteractionEnabled = YES;
    buttonImage3.multipleTouchEnabled = YES;
    [scrollview addSubview:buttonImage3];
}


- (void)wasDragged:(UIButton *)buttonImage withEvent:(UIEvent *)event{
    // get the touch
    UITouch *touch = [[event touchesForView:buttonImage] anyObject];

    // get delta
    CGPoint previousLocation = [touch previousLocationInView:buttonImage.superview];
    CGPoint location = [touch locationInView:buttonImage.superview];    CGFloat delta_x = location.x - previousLocation.x;
    CGFloat delta_y = location.y - previousLocation.y;

    // move button
    buttonImage.center = CGPointMake(buttonImage.center.x + delta_x,buttonImage.center.y + delta_y);
    NSLog(@"button was clicked");

}

1 个答案:

答案 0 :(得分:0)

   //Scale to new Frame:
    CGRect newFrameAfterScale = buttonImage.frame;
    newFrameAfterScale.size = CGSizeMake(buttonImage.frame.size.width*2,buttonImage.frame.size.height*2);

   [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
       //
       //Scale:
       [buttonImage setFrame:newFrameAfterScale];
       //
       //Roatate:
       buttonImage.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI *(+90/ 180.0));


   } completion:^(BOOL finished) {
       //
   }];

希望这有帮助