如何使用滑动手势从目标c中的锚点旋转图像?

时间:2015-02-25 11:35:56

标签: ios objective-c xcode

在这里,我旋转给定角度的图像。但是我想用一个定位点的滑动手势旋转图像。这是我的代码..

    - (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];
}

2 个答案:

答案 0 :(得分:1)

如果您想要用手指动态旋转图像,则必须使用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

<强> UPDATE2

我已经在github上创建了带有示例的类(和类别): https://github.com/faviomob/UIRotatableView

答案 1 :(得分:0)

您可以使用它将图像旋转90度。

CGAffineTransformMakeRotation(CGFloat(M_PI_2))