基于触摸问题的旋转

时间:2011-03-10 07:17:07

标签: iphone rotation

我正在制作一个简单的表盘,当您将手指拖过它时会旋转。它旋转得很好,但是当我触摸屏幕上的任何地方并拖动我的手指时它也会旋转。

如何限制第一次触摸仅限于我的imageview对象?或者我哪里错了?

这是我的麻烦代码:

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {

        UIImage *image1 = [UIImage imageNamed:@"nav@2x.png"];

        wheelfrom = [[UIImageView alloc] initWithImage:image1];
        wheelfrom.frame =CGRectMake(10, -130, 300, 300);


        [self addSubview:wheelfrom];

    }
    return self;
}



-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch =[[[event allTouches] allObjects] lastObject];   
    firstLoc = [touch locationInView:self];

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch =[[[event allTouches] allObjects] lastObject];
    CGPoint curLoc = [touch locationInView:self];


    float fromAngle = atan2( firstLoc.y-wheelfrom.center.y, 
                            firstLoc.x-wheelfrom.center.x );
    float toAngle = atan2( curLoc.y-wheelfrom.center.y, 
                          curLoc.x-wheelfrom.center.x );

    float newAngle = angle + (toAngle - fromAngle);

    CGAffineTransform cgaRotate = CGAffineTransformMakeRotation(newAngle);
    wheelfrom.transform = cgaRotate;


angle = newAngle;   
}

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

你试试这个,

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];

    CGPoint location = [touch locationInView:self.view];


    if(CGRectContainsPoint(wheelfrom.frame, location))
    {
         //do your things            
    }
}

答案 1 :(得分:0)

您可以尝试检查触摸点是否在图像视图的框架内。只有当它是的时才做你想​​要的。

答案 2 :(得分:0)

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event内,检查firstLoc是否在您的范围内。