ios 圖片旋轉放大加移動

來源:互聯網
上載者:User

標籤:ios   ios開發   手勢觸摸   移動   uiimageview   

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


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


我們建立一個工程

#import "ViewController.h"


@interface ViewController ()<UIGestureRecognizerDelegate>

{

    UIImageView *_imageView;

}


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];


    

    [selfcreateImageView];

    [selfcreatePinchGesture];

    [selfcreateRotataeGesture];

//    [self createPanGesture];

}


//建立縮放手勢

-(void)createPinchGesture

{

    

    UIPinchGestureRecognizer *pinchGes  =[[UIPinchGestureRecognizeralloc]init];

    

    pinchGes .delegate =self;

    

    [pinchGes addTarget:selfaction:@selector(pinchGes:)];

    [_imageViewaddGestureRecognizer: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 = [[UIRotationGestureRecognizeralloc]init];

    

    rotationGes.delegate =self;

    

    [rotationGes addTarget:selfaction:@selector(rotGes:)];

    [_imageViewaddGestureRecognizer:rotationGes];

}

-(void)rotGes:(UIRotationGestureRecognizer *)ges

{

    

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

    ges.rotation =0;

    

}

-(void)createImageView

{

    UIImageView *imageView = [[UIImageViewalloc]init];

    

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

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

    

    imageView.userInteractionEnabled =YES;

    

    [self.viewaddSubview:imageView];

    _imageView = imageView;

}


//計算位移量移動圖片

//第一種計算方式

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

{

    

    [[selfclass] cancelPreviousPerformRequestsWithTarget:self];

    

    //取到當前觸摸對象

    UITouch *touch = [touchesanyObject];

    

    //當前位置

    CGPoint point = [touchlocationInView:self.view];

    //之前的位置

    CGPoint prePoint = [touchpreviousLocationInView:self.view];

    

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

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

    

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

    CGPoint center =_imageView.center;

    //把計算出來的位移量,就是imagview的x軸加上算出來的位移量,如是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

{

    returnYES;

}

@end





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




ios 圖片旋轉放大加移動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.