iOS-觸摸手勢

//演示手勢的使用
// 手勢是觸摸的封裝

    //學習6種手勢
    //UITapGestureRecognizer        輕擊
    //[self testTap];
    //UIPanGestureRecognizer        拖動
    //[self testPan];
    
    //UIRotationGestureRecognizer   旋轉
    //[self testRotation];
    
    //UIPinchGestureRecognizer      縮放
    //[self testPinch];
    
    //UISwipeGestureRecognizer      滑動手勢
    //[self testSwipe];
    
    //UILongPressGestureRecognizer  長按手勢
    [self testLongPress];
-(void)testLongPress
{
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(dealLongPress:)];
    [_imageView addGestureRecognizer:longPress];
}
-(void)dealLongPress:(UILongPressGestureRecognizer *)longPress
{
    NSLog(@"longPress");
    //注意: 這個事件處理方法會執(zhí)行兩次
    if(longPress.state == UIGestureRecognizerStateBegan
       )
    {
        //顯示對話框
        UIAlertView *alertView = [[UIAlertView alloc] init];
        alertView.message = @"真的要保存嘛?";
        [alertView addButtonWithTitle:@"確定"];
        [alertView show];
    }
}

-(void)testSwipe
{
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(dealSwipe:)];
    //默認: 從左到右滑動生效
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [_imageView addGestureRecognizer:swipe];
}
-(void)dealSwipe:(UIPinchGestureRecognizer *)swipe
{
    NSLog(@"swipe");
    
    //
    _index++;
    if(_index == _images.count)
    {
        _index = 0;
    }
    _imageView.image = [UIImage imageNamed:_images[_index]];

}
-(void)testPinch
{
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(dealPinch:)];
    [_imageView addGestureRecognizer:pinch];
}
-(void)dealPinch:(UIPinchGestureRecognizer *)pinch
{
    //
    double scale = pinch.scale;
    _imageView.transform = CGAffineTransformMakeScale(scale, scale);
}

-(void)testRotation
{
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(dealRotation:)];
    [_imageView addGestureRecognizer:rotation];
}
-(void)dealRotation:(UIRotationGestureRecognizer *)rotation
{
    //獲取旋轉的角度(弧度)
    double r = rotation.rotation;
    NSLog(@"r = %f",r);
    
    //對視圖進行旋轉
    // 仿射變換對象 CGAffineTransform
    _imageView.transform = CGAffineTransformMakeRotation(r);
    
}
-(void)testPan
{
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dealPan:)];
    [_imageView addGestureRecognizer:pan];
}
-(void)dealPan:(UIPanGestureRecognizer *)pan
{
    //CGPoint point = [pan locationInView:self.view];
    //_imageView.center = point;
    
    //開始移動的時候記錄視圖的中心點
    if(pan.state == UIGestureRecognizerStateBegan)
    {
        _startPoint = _imageView.center;
    }
    //移動的時候獲取, 移動開始點和當前的點得偏移
    //  imageView視圖位置, imageView開始位置 + offset
    if(pan.state == UIGestureRecognizerStateChanged)
    {
        //
        CGPoint offset = [pan translationInView:self.view];
        _imageView.center = CGPointMake(_startPoint.x+offset.x, _startPoint.y + offset.y);
        
    }
}

-(void)testTap
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dealTap:)];
    [_imageView addGestureRecognizer:tap];
    
    //
    
}
//事件處理方法參數(shù)一般有一個, 參數(shù)類型是事件源
-(void)dealTap:(UITapGestureRecognizer *)tap
{
    NSLog(@"tap");
    
    //獲取點擊位置
    //傳入一個參考view, 以這個view左上角點原點,獲取位置
    CGPoint point = [tap locationInView:_imageView];
    NSLog(@"x=%f y=%f",point.x,point.y);

}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容