你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS開發從入門到精通--UIGesture手勢,圖片的縮放和旋轉

iOS開發從入門到精通--UIGesture手勢,圖片的縮放和旋轉

編輯:IOS開發綜合

UIGesture手勢,圖片的縮放和旋轉

#import 

@interface ViewController : UIViewController{

    //定義一個縮放手勢,用來對視圖可以進行放大或者縮小
    //Pinch:捏合手勢
    UIPinchGestureRecognizer * _pinchGes;

    //定義一個旋轉手勢,主要用來旋轉圖像視圖
    UIRotationGestureRecognizer * _rotGes;
}


@end
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIImage * image =[UIImage imageNamed:@"17_3.png"];

    UIImageView * iView = [[UIImageView alloc]initWithImage:image];

    iView.frame=CGRectMake(50, 80, 200, 300);

    //開啟交互功能
    iView.userInteractionEnabled=YES;

    [self.view addSubview:iView];

    //創建一個捏合手勢
    //p1:事件對象的擁有者
    //p2:事件響應函數
    _pinchGes =[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAct:)];

    //將捏合手勢添加到視圖中
    [iView addGestureRecognizer:_pinchGes];


    //創建旋轉手勢
    _rotGes =[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotAct:)];

    [iView addGestureRecognizer:_rotGes];

    //設置手勢的代理
    _rotGes.delegate=self;
    _pinchGes.delegate=self;

}

//是否可以同時相應兩個手勢
//如果返回值為YES:可以同時相應
//如果返回值為NO:不可以同時相應
-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

//旋轉手勢函數
-(void)rotAct:(UIRotationGestureRecognizer*)rot{
    UIImageView* iView = (UIImageView*)rot.view;
    //計算旋轉的變換矩陣並且賦值
    iView.transform = CGAffineTransformRotate(iView.transform,rot.rotation);
    //選擇角度清零
    rot.rotation=0;
}

//捏合手勢事件函數實現
-(void)pinchAct:(UIPinchGestureRecognizer*)pinch{
    //獲取監控圖像視圖
    UIImageView * iView =(UIImageView*)pinch.view;
    //對圖像視圖對象進行矩陣變換計算並賦值
    //transform:表示圖形學中的變換矩陣
    //CGAffineTransformScale:通過縮放的方式產生一個新矩陣
    //參數一:原來的矩陣
    //參數二:x方向的縮放比例
    //參數三:y方向的縮放比例
    //返回值是新的縮放後的矩陣變換
    iView.transform = CGAffineTransformScale(iView.transform, pinch.scale, pinch.scale);

    //將縮放值歸位為單位值
    //scale=1:原來的大小
    //scale<1:縮小效果
    //scale>1:放大效果
    pinch.scale=1;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved