开发者

Rotation based on touch problem

I'm making a simple dial that rotates as you drag your finger across it. It rotates great, but it开发者_开发技巧 also rotates when i touch anywhere on the screen and drag my finger.

How can i restrict the first touches to be only inside my imageview object? or where am i going wrong?

this is my code of trouble:

- (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;   
}

Thanks for your help!


You try like this,

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


You can try by checking if the point of touch is within the frame of the image view.Do what you want only if its yes.


Inside -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event, check the firstLoc is within your range.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜