iPhone - 车轮跟踪手指跳到相同的起始位置? [英] iPhone - Wheel tracking finger jumps to same starting position?

查看:109
本文介绍了iPhone - 车轮跟踪手指跳到相同的起始位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在iphone应用程序中实现选择轮。我有车轮跟踪用户的手指,但现在由于某种原因,当用户触摸屏幕时,车轮会跳跃,因此每次轮子的相同部分都会跟踪手指。

I am trying to implement a selection wheel in an iphone application. I have got the wheel to track the users finger but now for some reason when the user touches the screen the wheel jumps so the same section of the wheel tracks the finger each time.

如何让轮子旋转,就像用户从任何位置拖动轮子一样?

How would I get it so the wheel rotates like the user is dragging it from any point?

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];

int tInput = [allTouches count]-1;

UITouch *touch =[[allTouches allObjects] objectAtIndex:tInput];
CGPoint location = [touch locationInView:self.view];
float theAngle = atan2( location.y-imageX.center.y, location.x-imageX.center.x );

CGAffineTransform cgaRotate = CGAffineTransformMakeRotation(theAngle);
imageX.transform = cgaRotate;

}

有任何建议吗?

推荐答案

对 - 这很有道理 - 这确实是你的代码所做的。

Right - that makes perfect sense - this is indeed what your code does.

您需要添加的是初始值,您可以将拖动作为相对旋转开始 - 或跟踪上一次旋转。

What you need to add is the initial value where you started your drag as a relative rotation -- or track your last rotation.

下面显示了一个非常复杂的方法 - 但这应该有助于说明这一点。

A very elaborate way is shown below - but this should help get the point across.

@interface UntitledViewController : UIViewController {
        CGPoint firstLoc;
        UILabel * fred;
        double angle;
}
@property (assign) CGPoint firstLoc;
@property (retain) UILabel * fred;

@implementation UntitledViewController
@synthesize fred,firstLoc;

- (void)viewDidLoad {
    [super viewDidLoad];

    self.fred = [[UILabel alloc] initWithFrame:CGRectMake(100,100,100,100)];
    fred.text = @"Fred!"; fred.textAlignment = UITextAlignmentCenter;
    [self.view addSubview:fred];

    angle = 0; // we aint have rotated just yet...
};

// make sure we get them drag events.
- (BOOL)isFirstResponder { return YES; } 

-(void)handleObject:(NSSet *)touches 
          withEvent:(UIEvent *)event 
             isLast:(BOOL)lst 
{
    UITouch *touch =[[[event allTouches] allObjects] lastObject];
    CGPoint curLoc = [touch locationInView:self.view];

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

    // So the angle to rotate to is relative to our current angle and the
    // angle through which our finger moved (to-from)
    float newAngle = angle + (toAngle - fromAngle);

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

    // we only 'save' the current angle when we're done with the drag.
    //
    if (lst)
        angle = newAngle;
    }

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch =[[[event allTouches] allObjects] lastObject];

    // capture where we started - so we can later work out the 
    // rotation relative to this point.
    //
    firstLoc = [touch locationInView:self.view];
};


-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [self handleObject:touches withEvent:event isLast:NO];
};

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self handleObject:touches withEvent:event isLast:YES];
}

显然你可以做得更优雅 - 而且上面错过了一点0 .. 2xPI封顶你需要。

Obviously you can do this a lot more elegant - and above misses a bit of 0 .. 2xPI capping you need.

这篇关于iPhone - 车轮跟踪手指跳到相同的起始位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆