开发者

iPhone wheel not running smoothly

开发者 https://www.devze.com 2023-01-17 16:59 出处:网络
I am implementing a wheel for iPhone which should turn clockwise or anti-clockwise upon being dragged into that direction. Although it does this, there was some errors with smoothness. Sometimes it se

I am implementing a wheel for iPhone which should turn clockwise or anti-clockwise upon being dragged into that direction. Although it does this, there was some errors with smoothness. Sometimes it seems like it get confused regards which way it should turn. Any help please?

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

    NSSet *allTouches = [event allTouches];

    int len = [allTouches count]-1;

    UITouch *touch =[[allTouches allObjects] objectAtIndex:len];
    CGPoint location = [touch locationInView:[self superview]];

    xfirst = location.x;
    xnext = xfirst;
    x = [NSNumber numberWithFloat:xfirst];
    yfirst = location.y;
    ynext = yfirst;
    y = [NSNumber numberWithFloat:yfirst];
    //NSLog(@"Touch began for wheel at %f and %f",xfirst, yfirst);
    //NSLog(@"Centre %f and %f",self.center.x, self.center.y);
}

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

    UITouch *touch =[[allTouches allObjects] objectAtIndex:len];
    CGPoint location = [touch locationInView:[self superview]];

    xlast = location.x;
    ylast = location.y;

//dy / dx (gradient)
    if (((ylast-yfirst)/(xlast-xfirst) >= 0.3) || ((ylast-yfirst)/(xlast-xfirst) <= -0.3))
    {

    int seg1 = [self detectSegment:xnext :ynext];
    int seg2 = [self detectSegment:xlast :ylast];
    bool direction = [self isClockWise:seg1 :seg2];
    lastDirection = direction;

    float theAngle = [self getAngle:self.center.x:self.center.y:xfirst:yfirst:xlast:ylast];

    theAngle = theAngle / 6; 

    if (direction == YES)
    {

    }
    els开发者_运维技巧e {

        theAngle = -1*theAngle;
    }

    theAngle += totalRadians;   
    float rads = 6.28318531;
    int divAm = 0;
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setMaximumFractionDigits:0];
    [formatter setRoundingMode: NSNumberFormatterRoundDown];

    float nm = theAngle/rads;
    NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:nm]];
    [formatter release];
    divAm = [numberString floatValue];
    theAngle = theAngle - (float)(6.28318531*divAm);
    totalRadians = theAngle;
    xnext = xlast;
    ynext = ylast;
    [self rotateImage:theAngle];
    }
}


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

-(float) getAngle: (CGFloat)P1x: (CGFloat)P1y: (CGFloat)P2x: (CGFloat)P2y: (CGFloat)P3x: (CGFloat)P3y {
    float P23 = [self getLength:P2x:P2y:P3x:P3y];
    float P12 = [self getLength:P1x:P1y:P2x:P2y];
    float P13 = [self getLength:P1x:P1y:P3x:P3y];
    return acos((P23*P23 - P12*P12 - P13*P13)/(-2*P12*P13));
}

-(float) getLength: (CGFloat)P1x: (CGFloat)P1y: (CGFloat)P2x: (CGFloat)P2y
{
    return sqrt((P1x-P2x)*(P1x-P2x) + (P1y-P2y)*(P1y-P2y));
}

Any suggestions please?


I'm a bit confused why you're using such a complex algorithm to find the angle. I don't understand why you need the gradient, and using number formatting in a calculation is a little strange. That said, you may have good reason for doing these things, so I suggest you simplify the algorithm in order to test the rotation code; you can add the complexity later if you still need it. Fundamentally, you just need:-

theAngle = atan2( location.y-self.center.y, location.x-self.center.x )

to ensure the wheel tracks your finger.

0

精彩评论

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