所以,
我正在尝试做一个非常简单的光盘旋转(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];
我也愿意接受新的/更清洁/更正确的方法。
提前致谢。