ios - 如何从 objective-c 中的 anchor 使用滑动手势旋转图像?

标签 ios objective-c xcode

在这里,我旋转给定角度的图像。但我想从 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/

相关文章:

iphone - 线程中的 CADisplayLink

iphone - 如何在appdelegate上检查横向和纵向模式?

ios - 基于 UIBezierPath 改变 UIImageView 的位置

swift - 如何在点击文本字段时突出显示文本

c++ - catch(...) 吞下 xcode llvm 3.0 中的所有其他捕获

ios - 点击完成后如何隐藏标签?

ios - 应用程序在模拟器中完美运行,但在 iPod Touch 上运行时出现 "Undefined Symbols"错误

javascript - 转换成json对象标签值格式

ios - 如何从字符串中获取 NSDate

javascript - 当我尝试运行 javascript 时,我发现一些警告,如何克服它