在这里,我旋转给定角度的图像。但我想从 anchor 用滑动手势旋转图像。这是我的代码..
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//circleName is an UIImageView IBoutlet
_circleNames=@"Squre.png";
_showCircle=[UIImage imageNamed:_circleNames];
_circleView.image=_showCircle;
// Here I am setting anchor point
_circleView.layer.anchorPoint=CGPointMake(0.5, 1.0);
//here I am setting animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:6];
[UIView setAnimationRepeatCount:0];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
//rotate 170 degree angle
_circleView.transform=CGAffineTransformRotate(_circleView.transform,170);
[UIView commitAnimations];
}
最佳答案
如果您想跟随手指动态旋转图像,您必须使用 UIPanGestureRecognizer
(不是 UISwipeGestureRecognizer
)。它让您反复调用您提供的选择器。
更新:
您可以使用此代码:
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView* rotView;
@end
@implementation ViewController {
UIPanGestureRecognizer* _panGesture;
float _deltaAngle;
CGAffineTransform _startTransform;
CGPoint _prevPoint;
}
- (void) viewDidLoad {
[super viewDidLoad];
_panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(rotateItem:)];
_panGesture.delegate = self;
[self.rotView addGestureRecognizer:_panGesture];
}
- (void) rotateItem:(UIPanGestureRecognizer *)recognizer {
CGPoint currPoint = [recognizer locationInView:self.view];
CGPoint center = recognizer.view.center;
CGFloat ang = atan2f(currPoint.y - center.y, currPoint.x - center.x) - atan2f(_prevPoint.y - center.y, _prevPoint.x - center.x);
_prevPoint = [recognizer locationInView:self.view];
_deltaAngle += ang;
self.rotView.transform = CGAffineTransformRotate(_startTransform, _deltaAngle);
}
- (BOOL) gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)recognizer {
_startTransform = self.rotView.transform;
return YES;
}
@end
更新 2
我在 github 上用示例创建了类(和类别): https://github.com/faviomob/UIRotatableView
关于ios - 如何从 objective-c 中的 anchor 使用滑动手势旋转图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28718011/