IOS開發之捕獲旋轉的手勢

來源:互聯網
上載者:User

1 前言

建立一個 UIRotationGestureRecognizer 的監視器,然後在綁定到你的視圖中,用來捕獲使用者利用手指在螢幕上做旋轉的手勢 。UIRotationGestureRecognizer 這個類有一個 rotation 的屬性,這個屬性可以用來設定旋轉的方向和旋轉的弧 度 。 當 用 戶 的 手 勢 開 始 和 結 束 的 時 候 分 別 會 用 到 如 下 的 兩 個 屬 性 .UIGestureRecognizerStateBegan, UIGestureRecognizerStateEnded.


2 代碼執行個體
ZYViewController.m

[plain]
//rotationGestureRecognizer 是我們的旋轉手勢的監聽捕獲執行個體對象。 
@synthesize rotationGestureRecognizer; 
@synthesize helloWorldLabel; 
@synthesize rotationAngleInRadians; 
 
- (void)viewDidLoad 

    [super viewDidLoad]; 
    self.view.backgroundColor = [UIColor whiteColor]; 
    /******添加Label組件 start******/ 
    self.helloWorldLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
    self.helloWorldLabel.text = @"Hello, World!"; 
    self.helloWorldLabel.font = [UIFont systemFontOfSize:16.0f]; 
    [self.helloWorldLabel sizeToFit]; 
    //置中 
    self.helloWorldLabel.center = self.view.center; 
    [self.view addSubview:self.helloWorldLabel]; 
    /******添加Label組件 end******/ 
    //UIRotationGestureRecognizer 手勢辨識器,這個類能用來監聽和捕獲旋轉的手勢,添加一個旋轉的手勢辨識器,這樣使用者就可以順利的使 用手勢來調整圖片的位置。 
    self.rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotations:)]; 
    //添加手勢 
    [self.view addGestureRecognizer:self.rotationGestureRecognizer]; 
     

 
- (void) handleRotations:(UIRotationGestureRecognizer *)paramSender{ 
    if (self.helloWorldLabel == nil){ return; 
    } 
    //利用 CGAffineTransformMakeRotation 這 個方法來進行一個旋轉手勢的事件傳遞.將前一個旋轉的弧度和當前的旋轉弧度相加 
    //rotationAngleInRadians 這個對象就是我們在旋轉的時候需要為我們的標籤對象設定的一個位置對象資訊,當我們每一次旋轉的時 候我們都會把一個新的位置值儲存在這個對象裡面,達到一個旋轉的效果。 
    self.helloWorldLabel.transform = CGAffineTransformMakeRotation(self.rotationAngleInRadians + paramSender.rotation); 
    //手勢結束時候,調整視圖位置 
    if (paramSender.state == UIGestureRecognizerStateEnded){ 
        self.rotationAngleInRadians += paramSender.rotation; 
    } 

//rotationGestureRecognizer 是我們的旋轉手勢的監聽捕獲執行個體對象。
@synthesize rotationGestureRecognizer;
@synthesize helloWorldLabel;
@synthesize rotationAngleInRadians;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    /******添加Label組件 start******/
    self.helloWorldLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.helloWorldLabel.text = @"Hello, World!";
    self.helloWorldLabel.font = [UIFont systemFontOfSize:16.0f];
    [self.helloWorldLabel sizeToFit];
    //置中
    self.helloWorldLabel.center = self.view.center;
    [self.view addSubview:self.helloWorldLabel];
    /******添加Label組件 end******/
    //UIRotationGestureRecognizer 手勢辨識器,這個類能用來監聽和捕獲旋轉的手勢,添加一個旋轉的手勢辨識器,這樣使用者就可以順利的使 用手勢來調整圖片的位置。
    self.rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotations:)];
    //添加手勢
    [self.view addGestureRecognizer:self.rotationGestureRecognizer];
   
}

- (void) handleRotations:(UIRotationGestureRecognizer *)paramSender{
    if (self.helloWorldLabel == nil){ return;
    }
    //利用 CGAffineTransformMakeRotation 這 個方法來進行一個旋轉手勢的事件傳遞.將前一個旋轉的弧度和當前的旋轉弧度相加
    //rotationAngleInRadians 這個對象就是我們在旋轉的時候需要為我們的標籤對象設定的一個位置對象資訊,當我們每一次旋轉的時 候我們都會把一個新的位置值儲存在這個對象裡面,達到一個旋轉的效果。
    self.helloWorldLabel.transform = CGAffineTransformMakeRotation(self.rotationAngleInRadians + paramSender.rotation);
    //手勢結束時候,調整視圖位置
    if (paramSender.state == UIGestureRecognizerStateEnded){
        self.rotationAngleInRadians += paramSender.rotation;
    }
}
運行結果

 
 


旋轉後(旋轉方法,按住Option鍵,按住滑鼠左鍵,出現兩個小圓球,然後拖動旋轉)結果

 
 


3 結語
以上是所有內容,希望對大家有所協助。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.