CGAffineTransformMakeRotation在180度(-3.14)之后走另一条路

时间:2013-10-26 21:48:55

标签: ios objective-c rotation

所以,

我正在尝试做一个非常简单的光盘旋转(2d),根据用户的触摸,就像DJ或其他东西。 它有效,但是有一个问题,经过一定的旋转后,它开始向后移动,这个数量是180度之后,或者当我记录角度时看到的,-3.14(pi)。

我想知道,我怎么能实现无限循环,我的意思是,用户可以保持旋转并旋转到任何一侧,只需滑动他的手指? 还有第二个问题是,有没有办法加快轮换?

这是我现在的代码:

#import <UIKit/UIKit.h>

@interface Draggable : UIImageView {
    CGPoint firstLoc;
    UILabel * fred;
    double angle;
}

@property (assign) CGPoint firstLoc;
@property (retain) UILabel * fred;

@end


@implementation Draggable

@synthesize fred, firstLoc;

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];

    angle = 0;
    if (self) {
        // Initialization code
    }
    return self;
}
-(void)handleObject:(NSSet *)touches
          withEvent:(UIEvent *)event
             isLast:(BOOL)lst
{
    UITouch *touch =[[[event allTouches] allObjects] lastObject];
    CGPoint curLoc = [touch locationInView:self];

    float fromAngle = atan2( firstLoc.y-self.center.y,
                            firstLoc.x-self.center.x );
    float toAngle = atan2( curLoc.y-(self.center.y+10),
                          curLoc.x-(self.center.x+10));
    float newAngle = angle + (toAngle - fromAngle);

    NSLog(@"%f",newAngle);

    CGAffineTransform cgaRotate = CGAffineTransformMakeRotation(newAngle);

    self.transform = cgaRotate;

    if (lst)
        angle = newAngle;
}

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


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

@end

在ViewController中:

UIImage *tmpImage = [UIImage imageNamed:@"theDisc.png"];
CGRect cellRectangle;
    cellRectangle = CGRectMake(-1,self.view.frame.size.height,tmpImage.size.width ,tmpImage.size.height );
    dragger = [[Draggable alloc] initWithFrame:cellRectangle];
    [dragger setImage:tmpImage];
    [dragger setUserInteractionEnabled:YES];

    dragger.layer.anchorPoint = CGPointMake(.5,.5);

    [self.view addSubview:dragger];

我愿意采用新的/更清洁/更正确的方式来做这件事。

提前致谢。

2 个答案:

答案 0 :(得分:1)

如果角度低于-180或高于180度,则翻转角度。请考虑以下touchesMoved实现:

@implementation RotateView

#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)

CGFloat angleBetweenLinesInDegrees(CGPoint beginLineA, CGPoint endLineA, CGPoint beginLineB, CGPoint endLineB)
{
    CGFloat a = endLineA.x - beginLineA.x;
    CGFloat b = endLineA.y - beginLineA.y;
    CGFloat c = endLineB.x - beginLineB.x;
    CGFloat d = endLineB.y - beginLineB.y;

    CGFloat atanA = atan2(a, b);
    CGFloat atanB = atan2(c, d);

    // convert radians to degrees
    return (atanA - atanB) * 180 / M_PI;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint curPoint  = [[touches anyObject] locationInView:self];
    CGPoint prevPoint = [[touches anyObject] previousLocationInView:self];

    // calculate rotation angle between two points
    CGFloat angle = angleBetweenLinesInDegrees(self.center, prevPoint, self.center, curPoint);

    // Flip
    if (angle > 180) {
        angle -= 360;
    } else if (angle < -180) {
        angle += 360;
    }

    self.layer.transform = CATransform3DRotate(self.layer.transform, DEGREES_TO_RADIANS(angle), .0, .0, 1.0);
}

@end

在视图的外边界拖动时,它会像旋转轮一样连续旋转。希望它有所帮助。

答案 1 :(得分:0)

你有一些问题:

1 - )

CGPoint curLoc = [touch locationInView:self];

firstLoc = [touch locationInView:self];

您正在转换视图,然后询问其中的触摸位置。您无法在旋转视图中获得正确的触摸位置。 让它们变得不变。 (例如将self.superview放入容器后)

2 - )

cellRectangle = CGRectMake(-1,self.view.frame.size.height,tmpImage.size.width ,tmpImage.size.height );

通过将self.view.frame.size.height作为CGRect的{​​{1}}参数传递,您将Draggable实例放在屏幕之外。