iOS经典

iOS图片如何旋转

2017-06-20  本文已影响221人  呆呆羞

iOS开发图片如何旋转。

代码分享:
点击进入

方法一

/关于M_PI
#define M_PI 3.14159265358979323846264338327950288
其实它就是圆周率的值,在这里代表弧度,相当于角度制 0-360 度,M_PI=180度
旋转方向为:顺时针旋转
(M_PI
0.5)表示旋转90度
*/

    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.frame = CGRectMake(50, 50, 200, 200);
    imageView.backgroundColor =[UIColor redColor];
    imageView.image = [UIImage imageNamed:@"750_2X"];
//顺时针旋转75度,M_PI为180度,要逆时针为   -Degree
    double Degree=75.0/180.0;
    CGAffineTransform transform= CGAffineTransformMakeRotation(M_PI* Degree);
    imageView.transform = transform;//旋转
    [self.view addSubview:imageView];
方法二

根据屏幕任意一点,旋转相应的角度

Snip20170707_6.png

如上图,点击屏幕上点B
屏幕frame起点绿色为大写(X=0,Y=0);
以屏幕的中心点frame(self.view.center.x,self.view.center.y)作为数学的坐标原点红色小写(x=0,y=0)。
命题:
知道屏幕任意一点B,点B在屏幕的frame(X,Y),让图片顺时针旋转到角CAB 的度数。
求角CAB的度数。
思路:数学中,知道直角三角形的边长a,b,c,就可以根据反三角函数来求角度。
1.求反三角函数,必须知道,如何求三角函数
2.如何求三角函数,必须知道,直角三角形的边长a,b,c
3.现在只知道该点B在屏幕的frame,和屏幕的中心点frame,固然可以求出直角三角的,a,b,c边长

程序如何实现:
#define UISCREEN_WEDTH     [UIScreen mainScreen].bounds.size.width
#import "ViewXuanZhuan.h"

@interface ViewXuanZhuan ()
{
    CGPoint clickCoordinate;
    UIImageView* ArrowView;
}
@end

@implementation ViewXuanZhuan

- (void)viewDidLoad {
    [super viewDidLoad];
//设置背景图片
    NSString *path = [[NSBundle mainBundle]pathForResource:@"1080_3X"ofType:@"png"];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    self.view.layer.contents = (id)image.CGImage;
    [self creatImage];
}
#pragma mark=======creatImage
-(void)creatImage{
    
    CGPoint screenCenterPoint=self.view.center;
    
    
    UIImage *pic1 = [ UIImage imageNamed:@"yk_03_3X.png"];
    
    ArrowView  = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,UISCREEN_WEDTH-40 , UISCREEN_WEDTH-40)];
    [ArrowView setCenter:screenCenterPoint];
    [ArrowView setImage:pic1];
    ArrowView.userInteractionEnabled=YES;
    [self.view addSubview:ArrowView];
    
}
//当有一个或多个手指触摸事件在当前视图或window窗体中响应
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    NSSet *allTouches = [event allTouches];    //返回与当前接收者有关的所有的触摸对象
    UITouch *touch = [allTouches anyObject];   //视图中的所有对象
    CGPoint point = [touch locationInView:[touch window]]; //返回触摸点在window中的当前坐标
    CGFloat x = point.x;
    CGFloat y = point.y;
    NSLog(@"屏幕touch=Frame(%f, %f)", x, y);
    //点B的数学坐标,注意正负
    clickCoordinate =CGPointMake(x-self.view.center.x,self.view.center.y-y);
    NSLog(@"数学上坐标的B(%f, %f)",clickCoordinate.x, clickCoordinate.y);
    
    
    [self rotation:clickCoordinate];
}
#pragma mark=======图片的旋转
-(void)rotation:(CGPoint)point{
    CGFloat a=point.x;
    CGFloat b=point.y;
    //勾股定理  sqrt(X)是X开根号  pow(X,n)是X的n次方
    CGFloat c=sqrt(pow(b, 2)+pow(a, 2));
    //三角函数cosA=角A邻边/斜边
    double cosA=b/c;
    //反三角函数
    double acosA=acos(cosA);
    //转换成角度
    double Degree = acosA/M_PI * 180;
    NSLog(@"角度Degree=%f",Degree);
    double Angle =Degree/180.0;
    if (a>=0) {
        //a>0说明在数学中的第二和第四限象,故顺时针旋转
        //CGAffineTransform transform= CGAffineTransformMakeRotation(acosA);
        CGAffineTransform transform= CGAffineTransformMakeRotation(M_PI*Angle);
        ArrowView.transform = transform;
        
    }else if (a<0){
        //a>0说明在数学中的第一和第三限象,故逆时针旋转
        //CGAffineTransform transform= CGAffineTransformMakeRotation(-acosA);
        CGAffineTransform transform= CGAffineTransformMakeRotation(M_PI*-Angle);
        ArrowView.transform = transform;
        
    }
    
}

总感觉我的做法有点绕,希望有更好的解决办法,一定要留言,谢谢您。

如果文章帮到您,喜欢点个赞,谢谢您。

文章内容出错,记得留言,感激不尽。

上一篇下一篇

猜你喜欢

热点阅读