图片旋转和复位类似于qq空间的图片

本文介绍了一种在 iOS 应用中实现图片旋转的方法,通过使用 UIRotationGestureRecognizer 和 UIImageView,可以实现在不同旋转角度下图片的平滑过渡及复位功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController

@synthesize recordedRotation;


- (void)viewDidLoad {

    image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];

    image.image = [UIImage imageNamed:@"2.jpg"];

    image.userInteractionEnabled = YES;

    image.contentMode = UIViewContentModeScaleAspectFit;

    image.autoresizingMask =

    UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

    [self.view addSubview:image];

    [image release];

    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(onRotation:)];

    [image addGestureRecognizer:rotation];

    [rotation release];

    deltaAngle = atan2(image.frame.origin.y+image.frame.size.height - image.center.y,

                       image.frame.origin.x+image.frame.size.width - image.center.x);

    [super viewDidLoad];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}


- (void)onRotation:(UIGestureRecognizer *)_rotationGes {

    float rotation1 = self.recordedRotation - ((UIRotationGestureRecognizer*)_rotationGes).rotation;//recordedRotation自己定义的double类型变量

    NSLog(@"rotation1 is %f rotation is %f",rotation1,((UIRotationGestureRecognizer*)_rotationGes).rotation);


    image.transform = CGAffineTransformMakeRotation(- rotation1 ); //self view

    if ([(UIRotationGestureRecognizer*)_rotationGes state] == UIGestureRecognizerStateEnded) {

        _lastRotation = 0.0f;

        self.recordedRotation = rotation1; //获取了 当前旋转的弧度值

        NSLog(@"recordedRotation is %f",recordedRotation);

        [self resetImageView:(UIRotationGestureRecognizer*)_rotationGes];

    }

    image.contentMode = UIViewContentModeScaleAspectFit;

}


//该函数算法提供当旋转角度不超过1/8度角将复位,超过将直接去旋转的位置

- (void)resetImageView:(UIRotationGestureRecognizer *)_rotaionGes {

    //rotaion顺时针为正逆时针为负

    CGFloat rotaion = _rotaionGes.rotation;

    CGFloat engle = rotaion / (M_PI * 2);

    CGFloat eng = (engle - (int)engle) * (M_PI * 2);

    eng = fabs(eng);

    CGFloat fabsRotain = fabs(rotaion);

    NSLog(@"recordedRotation1 is %f eng is %f",recordedRotation,eng);

    if (fabsRotain > 0 && fabsRotain <= M_PI / 8) {

        if (rotaion > 0)

            self.recordedRotation = self.recordedRotation + eng;

        else if (rotaion < 0) {

            self.recordedRotation = self.recordedRotation - eng;

        }

    } else if (fabsRotain > M_PI / 8 && fabsRotain <= M_PI / 2) {

        if (rotaion < 0)

            self.recordedRotation = self.recordedRotation + (M_PI / 2 - eng);

        else

            self.recordedRotation = self.recordedRotation - (M_PI / 2 - eng);

    } else if (fabsRotain > M_PI / 2 && fabsRotain <= M_PI * 5 / 8) {

        if (rotaion < 0)

            self.recordedRotation = self.recordedRotation -  (- M_PI / 2  + eng);

        else

            self.recordedRotation = self.recordedRotation + (- M_PI / 2  + eng);

    } else if (fabsRotain > M_PI * 5 / 8 && fabsRotain <= M_PI) {

        if (rotaion < 0) {

            self.recordedRotation = self.recordedRotation + (M_PI - eng);

        } else

            self.recordedRotation = self.recordedRotation - (M_PI - eng);

    } else if (fabsRotain > M_PI  && fabsRotain <= M_PI * 9 / 8) {

        if (rotaion < 0) {

            self.recordedRotation = self.recordedRotation - (- M_PI + eng);

        } else {

            self.recordedRotation = self.recordedRotation + (- M_PI + eng);

        }

    } else if (fabsRotain > M_PI * 9 / 8 && fabsRotain <= M_PI * 3 / 2) {

        if (rotaion < 0) {

            self.recordedRotation = self.recordedRotation + (M_PI * 3 / 2 - eng);

        } else

            self.recordedRotation = self.recordedRotation - (M_PI * 3 / 2 - eng);

    } else if (fabsRotain > M_PI * 3 / 2 && fabsRotain <= M_PI * 13 / 8) {

        if (rotaion < 0) {

            self.recordedRotation = self.recordedRotation - (- M_PI * 3 / 2 + eng);

        } else

            self.recordedRotation = self.recordedRotation + (- M_PI * 3 / 2 + eng);

    } else if (fabsRotain > M_PI * 13 / 8 && fabsRotain <= M_PI *2) {

        if (rotaion < 0) {

            self.recordedRotation = self.recordedRotation + (M_PI * 2 - eng);

        } else

            self.recordedRotation = self.recordedRotation - (M_PI * 2 - eng);

    }

    NSLog(@"recordedRotation2 is %f eng is %f",recordedRotation,eng);

    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    [UIView setAnimationRepeatCount:1];

    [UIView setAnimationDuration:0.3f];

    [UIView setAnimationDelegate:self];

    [UIView setAnimationDidStopSelector:@selector(stopAnimated)];

    image.transform = CGAffineTransformMakeRotation(-self.recordedRotation);

    [self repositionImageView];


    [UIView commitAnimations];

}


- (void)stopAnimated {

  

}


- (void)repositionImageView {

    [image setFrame:CGRectMake(

                                        0,

                                        0,

                                        self.view.frame.size.width - (0 * 2),

                                        self.view.frame.size.height - (0 * 2))];

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值