【问题标题】:Imageview rotation issues in iOSiOS中的Imageview旋转问题
【发布时间】:2013-09-19 01:21:15
【问题描述】:

我有下一个任务:按钮点击图像必须旋转到 90 度。

我的代码是:

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *vector;
@end

=========

@interface ViewController ()

@end

@implementation ViewController
@synthesize vector;

- (void)viewDidLoad
{
    [super viewDidLoad];
}
- (IBAction)rotateOn:(id)sender {
    [self rotateImage:self.vector duration:2
                curve:UIViewAnimationCurveEaseIn degrees:M_PI/2];
}

- (void)rotateImage:(UIImageView *)image duration:(NSTimeInterval)duration
              curve:(int)curve degrees:(CGFloat)degrees
{
    // Setup the animation
    [UIView beginAnimations:NULL context:NULL];
    [UIView setAnimationDuration:duration];
    [UIView setAnimationCurve:curve];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [image.layer setAnchorPoint:CGPointMake(0.5,0.5)];
    CGAffineTransform transform =
    CGAffineTransformMakeRotation(degrees);
    image.transform = transform;
    [UIView commitAnimations];
}




在单击按钮之前,我有这个




点击这个后

您可以看到图像旋转了 90 度。但它从起始中心点向下移动。 此外,如果我再次单击按钮,则不会发生任何事情。

【问题讨论】:

    标签: ios image button rotation quartz-2d


    【解决方案1】:

    在你的旋转按钮方法中使用此代码将图像旋转90

    static int numRot = 0;
    myimage.transform = CGAffineTransformMakeRotation(M_PI_2 * numRot);
    ++numRot;
    

    【讨论】:

      【解决方案2】:

      使用:

      imageView.transform = CGAffineTransformMakeRotation(0);//Upright
      imageView.transform = CGAffineTransformMakeRotation(M_PI/2);//90 degrees clockwise
      imageView.transform = CGAffineTransformMakeRotation(-M_PI/2);//90 degrees counter-clockwise
      

      另外,检查你的框架

      • 点击按钮之前
      • 点击按钮后,旋转前
      • 旋转后

      您可能会在那里发现异常情况。

      【讨论】:

        【解决方案3】:

        我假设您使用的是 iOS6 和情节提要,请尝试在情节提要中禁用自动布局并再次测试动画。
        如果它有效,并且想要保留自动布局功能,您将需要调整您的约束

        顺便说一句,默认的anchorPoint已经是(0.5, 0.5)了,这一行是没有必要的(除非你在别处修改了anchorPoint):

        [image.layer setAnchorPoint:CGPointMake(0.5,0.5)];
        

        【讨论】:

        • 我是白痴 :) 我怎么能忘记约束!?!禁用自动布局是解决更改起始位置的问题
        • 禁用自动布局不是解决问题的方法,而是一种技巧。我遇到了同样的问题,我不知道如何“调整我的约束”。我将图像视图置于超级视图的中心,并将其固定到底部布局指南,并且具有固定的宽度和高度。当我旋转时,是什么限制导致了这个中心偏移?我通过在图像视图上将 translatesAutoresizingMaskIntoConstraints 设置为 true 来破解它。这并不能解决真正的问题。
        【解决方案4】:

        删除这一行:

          [image.layer setAnchorPoint:CGPointMake(0.5,0.5)];
        

        它实际上并没有做任何事情,因为 {0.5,0.5} 是默认值和中心点。您一定一直在使用不同的值(例如 {0,0})来获得偏心旋转。

        要在每次单击按钮时增加旋转,您需要使用累积变换。 “Make...”转换是上下文无关的——它们不考虑以前的转换。试试这个:

        CGAffineTransform transform; 
        transform = CGAffineTransformRotate(image.transform, degrees);
        image.transform = transform;
        

        然后,您将 degrees 旋转添加到当前图像的变换中,而不是将变换重置为新值。

        【讨论】:

        • 谢谢。你说的对! CGAffineTransformRotate(Transform, Degrees) 确实旋转图像不止一次。
        猜你喜欢
        • 2018-02-03
        • 1970-01-01
        • 1970-01-01
        • 2017-12-03
        • 1970-01-01
        • 1970-01-01
        • 2016-08-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多