图片的偏移和旋转问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图片的偏移和旋转问题相关的知识,希望对你有一定的参考价值。

#import "ViewController.h"

@interface ViewController ()
{
    UIImageView *imgView;
    int x;
    int y;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initWithFrame];
    
}

#pragma mark 初始化界面
- (void)initWithFrame
{
    imgView = [[UIImageView alloc]initWithFrame:(CGRect){100,100,100,100}];
    [imgView setImage:[UIImage imageNamed:@"header.jpg"]];
    [self.view addSubview:imgView];
    
    UIButton *upBtn = [[UIButton alloc]initWithFrame:(CGRect){100,300,100,30}];
    [upBtn setTitle:@"向上" forState:UIControlStateNormal];
    [upBtn addTarget:self action:@selector(upBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [upBtn setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:upBtn];
    
    UIButton *downBtn = [[UIButton alloc]initWithFrame:(CGRect){100,350,100,30}];
    [downBtn setTitle:@"向下" forState:UIControlStateNormal];
    [downBtn addTarget:self action:@selector(downBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [downBtn setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:downBtn];
    
    UIButton *leftBtn = [[UIButton alloc]initWithFrame:(CGRect){220,300,100,30}];
    [leftBtn setTitle:@"向左" forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [leftBtn setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:leftBtn];
    
    UIButton *rightBtn = [[UIButton alloc]initWithFrame:(CGRect){220,350,100,30}];
    [rightBtn setTitle:@"向右" forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:rightBtn];
    
    UIButton *rotateBtn = [[UIButton alloc]initWithFrame:(CGRect){100,400,100,30}];
    [rotateBtn setTitle:@"旋转" forState:UIControlStateNormal];
    [rotateBtn addTarget:self action:@selector(rotateBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [rotateBtn setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:rotateBtn];
    
    x = imgView.frame.origin.x;
    y = imgView.frame.origin.y;
}

以上是界面初始化,就不多讲了,新手版。

以下内容包括了个人见解和排错代码,当然,方式仅限于新手,如果有更好的方式还请留言,以供学习,交流,不甚感激。

/*
    过程中的注意点:
    使用transform时,要注意到transform的参考方位是 {a,b,c,d,tx,ty},旋转中心点以tx,ty为准,如果同时使用transform来进行平移和旋转就会造成,偏移的中心点无法估计,因为旋转时的角度改变了transform的tx,ty。
    所以一般在进行使用时,比较容易的方式是偏移用frame进行定位,旋转使用transform.
 
 */
#pragma  mark 向上按钮事件
- (void)upBtnAction:(id)sender
{

    [UIView animateWithDuration:1.0 animations:^{
////        imgView.transform = CGAffineTransformMakeTranslation( imgView.transform.tx, -50);
//        imgView.transform = CGAffineTransformTranslate(imgView.transform, 0, -50 );
        imgView.frame=CGRectMake(x,y-=50, 100,100);
    }];
//        NSLog(@"向上方位:%f,%f,%f,%f,%f,%f",imgView.transform.a,imgView.transform.b,imgView.transform.c,imgView.transform.d,imgView.transform.tx,imgView.transform.ty);

}


#pragma  mark 向左按钮事件
- (void)leftBtnAction:(id)sender
{
    [UIView animateWithDuration:1.0 animations:^{
////        imgView.transform = CGAffineTransformMakeTranslation( -50, imgView.transform.ty);
//        imgView.transform =  CGAffineTransformTranslate(imgView.transform, -50, 0 );
//
        imgView.frame=CGRectMake(x-=50,y, 100,100);
    }];
//    NSLog(@"向左方位:%f,%f,%f,%f,%f,%f",imgView.transform.a,imgView.transform.b,imgView.transform.c,imgView.transform.d,imgView.transform.tx,imgView.transform.ty);
}

#pragma  mark 向下按钮事件
- (void)downBtnAction:(id)sender
{
        [UIView animateWithDuration:1.0 animations:^{
            //        imgView.transform = CGAffineTransformMakeTranslation( imgView.transform.tx, 50);
//           imgView.transform =  CGAffineTransformTranslate(imgView.transform,  0, 50 );
            imgView.frame=CGRectMake(x,y+=50, 100,100);

        }];
    //
    //    NSLog(@"向下方位:%f,%f,%f,%f,%f,%f",imgView.transform.a,imgView.transform.b,imgView.transform.c,imgView.transform.d,imgView.transform.tx,imgView.transform.ty);

}

#pragma  mark 向右按钮事件
- (void)rightBtnAction:(id)sender
{
    [UIView animateWithDuration:1.0 animations:^{
////        imgView.transform = CGAffineTransformMakeTranslation( 50, imgView.transform.ty);
//       imgView.transform = CGAffineTransformTranslate(imgView.transform, 50, 0 );
    
        imgView.frame=CGRectMake(x+=50,y, 100,100);
    }];
//    NSLog(@"向右方位:%f,%f,%f,%f,%f,%f",imgView.transform.a,imgView.transform.b,imgView.transform.c,imgView.transform.d,imgView.transform.tx,imgView.transform.ty);

}

#pragma  mark 旋转按钮事件
- (void)rotateBtnAction:(id)sender
{
    
    //CGAffineTransform trans = CGAffineTransformMakeTranslation(0, 0);
        [UIView animateWithDuration:4 animations:^{
            imgView.transform = CGAffineTransformRotate(imgView.transform, M_PI);
        }];
 
}

 

以上是关于图片的偏移和旋转问题的主要内容,如果未能解决你的问题,请参考以下文章

html5如何让图片3d旋转

Jekyll 偏移代码片段高亮的初始行

记录一个移动端图片预览(支持旋转),使用css强制旋转的坑

使用 ActionBar 旋转 Android 的双片段

iOS8中设备旋转后的ViewController偏移量

iOS 带有偏移的垂直旋转木马