Skip to content

Instantly share code, notes, and snippets.

@Changzw
Last active September 21, 2018 05:10
Show Gist options
  • Save Changzw/e60d92f9a9d830030935bceb159c5657 to your computer and use it in GitHub Desktop.
Save Changzw/e60d92f9a9d830030935bceb159c5657 to your computer and use it in GitHub Desktop.
the method of realizing one finger rotation
@interface RotationView()
@property (nonatomic, strong, readonly) UILabel *rotationView;
@property (nonatomic, strong, readonly) UIPanGestureRecognizer *pan;
@property (nonatomic, assign) CGPoint previousPoint;
@property (nonatomic, assign) CGPoint currentPoint;
@end
_rotationView.userInteractionEnabled = YES;
_pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[_rotationView addGestureRecognizer:_pan];
- (void)rotation{
CGRect rect = self.rotationView.frame;
CGPoint centerPt = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
float prevAngle = atan2f(self.previousPoint.y - centerPt.y, self.previousPoint.x - centerPt.x);
float currAngle = atan2f(self.currentPoint.y - centerPt.y, self.currentPoint.x - centerPt.x);
float deltaAngle = currAngle - prevAngle;
NSLog(@"pan-- curr:%.2f, prev:%.2f, delta:%.2f", currAngle, prevAngle, deltaAngle);
self.rotationView.transform = CGAffineTransformRotate(self.rotationView.transform, deltaAngle);
}
- (void)pan:(UIPanGestureRecognizer *)pan{
CGPoint pt = [pan locationInView:self];
switch (pan.state) {
case UIGestureRecognizerStateBegan:
self.previousPoint = pt;
break;
case UIGestureRecognizerStateChanged:
self.currentPoint = pt;
[self rotation];
self.previousPoint = self.currentPoint;
break;
case UIGestureRecognizerStatePossible: break;
case UIGestureRecognizerStateEnded: break;
case UIGestureRecognizerStateCancelled: break;
case UIGestureRecognizerStateFailed: break;
}
[pan setTranslation:CGPointZero inView:self];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
CGPoint prevPt = [touches.anyObject previousLocationInView:self];
CGPoint currPt = [touches.anyObject locationInView:self];
CGPoint centerPt = self.rotationV.frame.origin;
float prevAngle = atan2f(prevPt.y - centerPt.y, prevPt.x - centerPt.x);
float currAngle = atan2f(currPt.y - centerPt.y, currPt.x - centerPt.x);
float deltaAngle = currAngle - prevAngle;
self.rotationV.transform = CGAffineTransformRotate(self.rotationV.transform, deltaAngle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment