iOS開發 UI學習筆記 UIGestureRecognizer手勢加載

手勢一共有七種,分別是輕拍,輕掃,旋轉,拖拽,捏合,長按,以及平移。
UIImageView *headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 02, 100, 100)];
    [headImageView setCenter:self.view.center];
    UIImage *headImage = [UIImage imageNamed:@"Icon.png"];
    headImageView.image = headImage;
    [headImageView setUserInteractionEnabled:YES];

首先初始化一個view來測試手勢。


添加輕拍手勢:

UITapGestureRecognizer


    /**
     輕拍手勢
     
     - parameter hiddenKeyBoard: 回調方法
     */
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenKeyBoard:)];

設置輕拍次數:

tapGesture.numberOfTouchesRequired = 2;

添加輕掃手勢

UISwipeGestureRecognizer

UISwipeGestureRecognizer *swipeTouche = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    //掃動方向
    [swipeTouche setDirection:(UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown)];

添加旋轉手勢:

UIRotationGestureRecognizer

    //旋轉手勢
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
    rotation.delegate = self;
    [headImageView addGestureRecognizer:rotation];

添加縮放手勢:

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
    pinch.delegate = self;
    [headImageView addGestureRecognizer:pinch];

添加平移手勢:

 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    pan.delegate = self;
    [headImageView addGestureRecognizer:pan];

添加長按手勢:

UILongPressGestureRecognizer *longPan = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPanAction:)];
    longPan.delegate = self;
    CFTimeInterval minTime = 1.00;
    longPan.minimumPressDuration = minTime;
    [headImageView addGestureRecognizer:longPan];

每個手勢添加的時候都有其回調方法;

手勢的代理:

<UIGestureRecognizerDelegate>

在代理方法裏可以實現多手勢的識別

#pragma mark - 代理方法
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    
    
    
    
    return YES;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章