使用TapGesture旋转UIImageView

时间:2011-02-12 01:40:37

标签: objective-c ios4 ipad

我需要一些帮助。如何通过点击屏幕来旋转UIImageView?

    - (void)viewDidLoad {

    UITapGestureRecognizer *tapgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self.view addGestureRecognizer:tapgr];
    [tapgr release];    
    [super viewDidLoad];
}

    -(void)tap:(UITapGestureRecognizer *)gesture {

    CGPoint touch = [gesture locationInView:self.view];
    CGPoint center = myImage.center;
    float dx,dy,wtf;
    dx = touch.x-center.x;
    dy = touch.y-center.y;
    wtf = atan2f(dy, dx);

    [self rotateImage:self.myImage withAngle:wtf];
}



     - (void)rotateImage:(UIImageView *)image withAngle:(float)newAngle
{
image.transform = CGAffineTransformMakeRotation(newAngle);


}

不要忽视 - 哪里出错?

1 个答案:

答案 0 :(得分:1)

我修复了我的代码并且工作正常

-(void)tap:(UITapGestureRecognizer *)gesture {

    CGPoint p = [gesture locationInView:self.view];

    CGPoint zero;
    zero.x = self.view.bounds.size.width / 2.0;
    zero.y = self.view.bounds.size.height / 2.0;

    CGPoint newPoint;

    newPoint.x = p.x - zero.x;
    newPoint.y = zero.y - p.y;
    CGFloat angle;
    angle = atan2(newPoint.x, newPoint.y);
    self.myImage.transform = CGAffineTransformRotate(CGAffineTransformIdentity, angle);

}