Last active
September 21, 2018 05:10
-
-
Save Changzw/e60d92f9a9d830030935bceb159c5657 to your computer and use it in GitHub Desktop.
the method of realizing one finger rotation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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