UI_手势

轻拍手势

    // 创建一个轻拍手势,同时绑定了事件
    UITapGestureRecognizer *aTapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGRAction:)];

    // 设置轻拍次数
    aTapGR.numberOfTapsRequired = 1;

    // 设置手指触摸的个数
    aTapGR.numberOfTouchesRequired = 1; // 模拟器按住 alt 可以两个手指触摸

    // 添加手势
    [self.rootView addGestureRecognizer:aTapGR];
    [aTapGR release];


#pragma mark - 轻拍手势
- (void)tapGRAction:(UITapGestureRecognizer *)sender
{
    [self.rootView.aTextField resignFirstResponder];
    // 下面也可以,让rootVIew 停止编辑
    // [self.rootView endEditing:YES];
}

长按手势

    UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGRAction:)];
    [self.rootView addGestureRecognizer:longPressGR];
    [longPressGR release];

#pragma mark - 长按手势
- (void)longPressGRAction:(UILongPressGestureRecognizer *)sender
{
    // 判断,否则会变两次
    if (sender.state == UIGestureRecognizerStateBegan) {
        CGFloat red = arc4random() % 256 / 255.0;
        CGFloat green = arc4random() % 256 / 255.0;
        CGFloat blue = arc4random() % 256 / 255.0;
        self.rootView.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];

    }
}

旋转手势

    UIRotationGestureRecognizer *rotationGR = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGRAction:)];
    // 手势要添加到图片上
    [self.rootView.opeImageView addGestureRecognizer:rotationGR];
    [rotationGR release];

#pragma mark - 旋转手势
- (void)rotationGRAction:(UIRotationGestureRecognizer *)sender
{
    // 操作对象和旋转角度
    self.rootView.opeImageView.transform = CGAffineTransformRotate(self.rootView.opeImageView.transform, sender.rotation);
    // 旋转后角度为0
    sender.rotation = 0;

    // 可以代替上面两行
    // self.rootView.opeImageView.transform = CGAffineTransformMakeRotation(sender.rotation);

}

捏合手势

    UIPinchGestureRecognizer *pinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGRAction:)];
    [self.rootView.opeImageView addGestureRecognizer:pinchGR];
    [pinchGR release];

#pragma mark - 捏合手势
- (void)pinchGRAction:(UIPinchGestureRecognizer *)sender
{
    self.rootView.opeImageView.transform = CGAffineTransformScale(self.rootView.opeImageView.transform, sender.scale, sender.scale);
    // 缩放完成后缩放比例重新置为1
    sender.scale = 1;

}

平移手势

    UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGRAction:)];
    [self.rootView.opeImageView addGestureRecognizer:panGR];
    [panGR release];


#pragma mark - 平移手势
- (void)panGRAction:(UIPanGestureRecognizer *)sender
{
    CGPoint point = [sender translationInView:sender.view];
    self.rootView.opeImageView.transform = CGAffineTransformTranslate(self.rootView.opeImageView.transform, point.x, point.y);

    // 平移之后让手势相对于所在的 view 重新置0
    [sender setTranslation:CGPointZero inView:sender.view];

}

轻扫手势

    UISwipeGestureRecognizer *swipeGR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGRAction:)];
    [self.rootView addGestureRecognizer:swipeGR];
    [swipeGR release];

    // 一个手势只能一个方向轻扫,多个方向写多个手势
    swipeGR.direction = UISwipeGestureRecognizerDirectionRight;
    /** typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) { UISwipeGestureRecognizerDirectionRight = 1 << 0, UISwipeGestureRecognizerDirectionLeft = 1 << 1, UISwipeGestureRecognizerDirectionUp = 1 << 2, UISwipeGestureRecognizerDirectionDown = 1 << 3 }; */

#pragma mark - 清扫手势
- (void)swipeGRAction:(UISwipeGestureRecognizer *)sender
{
    NSLog(@"清扫手势");
}

屏幕边缘轻扫

    UIScreenEdgePanGestureRecognizer *screenEdgePanGR = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanGRAction:)];
    [self.rootView addGestureRecognizer:screenEdgePanGR];
    [screenEdgePanGR release];

#pragma mark - 屏幕边缘清扫手势
- (void)screenEdgePanGRAction:(UIScreenEdgePanGestureRecognizer *)sender
{
    NSLog(@"屏幕边缘清扫手势");
}


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

你可能感兴趣的:(UI,事件,设置,手势)