ios 圖片旋轉放大加移動

今天這更有點遲了,讓大家久等了。哈哈,今天好玩的又來了,我們來玩一玩,怎麼把一張照片,旋轉放大加移動。很期待吧,那麼事不宜遲我們來實現它們。


說到這些功能,在ios中實現無非就是手勢觸摸功能,加上計算偏移,算出座標。就ok了。非常的簡單。


我們新建一個工程

#import "ViewController.h"


@interface ViewController ()<UIGestureRecognizerDelegate>

{

    UIImageView *_imageView;

}


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];


    

    [self createImageView];

    [self createPinchGesture];

    [self createRotataeGesture];

//    [self createPanGesture];

}


//創建縮放手勢

-(void)createPinchGesture

{

    

    UIPinchGestureRecognizer *pinchGes  =[[UIPinchGestureRecognizer alloc]init];

    

    pinchGes .delegate = self;

    

    [pinchGes addTarget:self action:@selector(pinchGes:)];

    [_imageView addGestureRecognizer:pinchGes];

}


-(void)pinchGes:(UIPinchGestureRecognizer *)ges

{

    //比列

    CGFloat scale = ges.scale;

    //改變視圖比列

    ges.view.transform = CGAffineTransformScale(ges.view.transform, scale, scale);

    //比列爲1

    ges.scale = 1;

}


//創建旋轉手勢

-(void)createRotataeGesture

{

    UIRotationGestureRecognizer *rotationGes = [[UIRotationGestureRecognizer alloc]init];

    

    rotationGes.delegate = self;

    

    [rotationGes addTarget:self action:@selector(rotGes:)];

    [_imageView addGestureRecognizer:rotationGes];

}

-(void)rotGes:(UIRotationGestureRecognizer *)ges

{

    

    ges.view.transform = CGAffineTransformRotate(ges.view.transform, ges.rotation);

    ges.rotation = 0;

    

}

-(void)createImageView

{

    UIImageView *imageView = [[UIImageView alloc]init];

    

    imageView.frame = CGRectMake(30, 60, 330, 500);

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

    

    imageView.userInteractionEnabled = YES;

    

    [self.view addSubview:imageView];

    _imageView = imageView;

}


//計算偏移量移動圖片

//第一種計算方式

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

    

    [[self class] cancelPreviousPerformRequestsWithTarget:self];

    

    //取到當前觸摸對象

    UITouch *touch = [touches anyObject];

    

    //當前位置

    CGPoint point = [touch locationInView:self.view];

    //之前的位置

    CGPoint prePoint = [touch previousLocationInView:self.view];

    

    //計算兩次點擊的偏移量

    CGPoint trans = CGPointMake(point.x - prePoint.x, point.y - prePoint.y);

    

    //修改imageview的結構(imageview的中心點 賦給center ,就是座標的變化)

    CGPoint center = _imageView.center;

    //把計算出來的偏移量,就是imagviewx軸加上算出來的偏移量,如是y軸不變,那就是0,不用加

    _imageView.center = CGPointMake(center.x + trans.x, center.y+ trans.y);

    

    //這個是點擊事件,因爲都已經經過準確的計算,每次都有精確的偏移量所以不需要清理上次的偏移量

    

    

}


//2種計算方式

//-(void)createPanGesture

//{

//    UIPanGestureRecognizer *panGse = [[UIPanGestureRecognizer alloc]init];

//    

//    panGse.delegate = self;

//  

//    [panGse addTarget:self action:@selector(panGes1:)];

//    [_imageView addGestureRecognizer:panGse];

//}

//-(void)panGes1:(UIPanGestureRecognizer *)ges

//{

//    CGPoint point = [ges translationInView:ges.view];

//    

//    ges.view.transform = CGAffineTransformTranslate(ges.view.transform, point.x, point.y);

//    

//    //偏移量清零(這個事手勢,如果每次手勢調用都不清理會自動累加到下次,造成不精確,所以每次都要清理上次的偏移量,重新計算下次的,保證精確)這個沒有經過準確的計算


////就是回到原先位置,然後從新計算,不然會累積到下次來

//    [ges setTranslation:CGPointZero inView:ges.view];

//    

//    

//}

//和其他手勢一起進行

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

    return YES;

}

@end





沒錯,這就是本公子了,哈哈。今天到此結束,大年30休息,(提前說下)就不更了,祝大家過個好年。




發佈了46 篇原創文章 · 獲贊 2 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章