我正在制作 iRetro 手机应用程序动画之类的拨号器动画。
对于 <180 度,拨号器在其原始位置逆时针正确移动。但是对于 > 180 度,它不起作用(逆时针未达到其原始位置)。这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
dialerImageView_Width = _dialer_View.bounds.size.width;
dialerImageView_Height= _dialer_View.bounds.size.height;
radians1 = atan2f(_dialer_View.transform.b, _dialer_View.transform.a);
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation_Start = [touch locationInView:self.view];
if (CGRectContainsPoint(_dialer_View.frame, touchLocation_Start)){
startTouch_Angle = [self getAngle:touchLocation_Start];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation_moved = [touch locationInView:self.view];
if (CGRectContainsPoint(_dialer_View.frame, touchLocation_moved)){
MovedTouch_Angle = [self getAngle:touchLocation_moved];
[_dialer_View setTransform:CGAffineTransformMakeRotation(startTouch_Angle-MovedTouch_Angle)];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation_End = [touch locationInView:self.view];
if (CGRectContainsPoint(_dialer_View.frame, touchLocation_End)){
EndTouch_Angle = [self getAngle:touchLocation_End];
// for < 180
if ((180/M_PI * (startTouch_Angle - EndTouch_Angle)) > 0.0f && (180/M_PI * (startTouch_Angle - EndTouch_Angle)) <= 180.0f )
{
[UIView animateWithDuration:0.5F animations:^{
[_dialer_View setTransform:CGAffineTransformMakeRotation(radians1)];
}];
}
// > 180
else {
[UIView animateWithDuration:0.5F animations:^{
[_dialer_View setTransform:CGAffineTransformMakeRotation(- 180 + radians1)];
//_dialer_View.transform = CGAffineTransformIdentity;
}];![enter image description here][1]
}
}
}
-(CGFloat) getAngle: (CGPoint) touchLocation
{
CGFloat x1 = _dialer_View.center.x;
CGFloat y1 = _dialer_View.center.y;
CGFloat x2 = touchLocation.x;
CGFloat y2 = touchLocation.y;
CGFloat baseAngle = atan2((x2-x1),(y2-y1));
return baseAngle;
}