UI之摇晃手机和触摸手势

摇晃手机

-(void)motionBegan:(UIEventSubtype)motionwithEvent:(UIEvent *)event//摇晃触发。[UIViewanimateWithDuration:0.1 animations:^{

//摇晃时的事件

}completion:^(BOOLfinished) {摇晃结束后引导的事件}

//-(void)motionEnded:(UIEventSubtype)motionwithEvent:(UIEvent *)event

//{

//    NSLog(@"摇晃结束");

//}

//-(void)motionCancelled:(UIEventSubtype)motionwithEvent:(UIEvent *)event

//{

//    NSLog(@"取消摇晃"); 

//}

种触摸手势

CATransform3D;//动画

@interfaceViewController ()<UIGestureRecognizerDelegate>//手势的协议

{

    UIImageView *_imageV;

}

@end

@implementationViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    _imageV = [[UIImageViewalloc]initWithFrame:CGRectMake(100, 200, 100, 100)];

    _imageV.image = [UIImageimageNamed:@"red"];

    _imageV.userInteractionEnabled = YES;

    [self.view addSubview:_imageV];

//    [self createTapGesture];

//    [self createLongPress];

//    [self createPanGesture];

//    [self createPinchGesture];

//    [self createRotationGesture];

    [self createSwipGesture];

}

 

-(void)createTapGesture{

//    UIGestureRecognizer 手势类 这是一个抽象基类,通常不会直接使用,我们会使用它的子类

   

    //UITapGestureRecognizer点击手势

    UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)];

    //创建手势需要给手势添加事件

    //点击次数

    tap.numberOfTapsRequired = 2;

    //需要几个手指点击

    tap.numberOfTouchesRequired = 1;

    //把手势添加在_imageV上

    [_imageV addGestureRecognizer:tap];

}

//注册(添加)的方法会把手势本身传过去

-(void)tapView:(UITapGestureRecognizer *)tap{

    NSLog(@"点击了");

    //拿到点击的控件

    UIView *view = tap.view;

    NSLog(@"%@",view);

   

    _imageV.image = [UIImageimageNamed:@"yellow"];

}

-(void)createLongPress{

    //长按手势

    UILongPressGestureRecognizer *longPress =[[UILongPressGestureRecognizer alloc]initWithTarget:selfaction:@selector(longPress:)];

    //最少长按时间

    longPress.minimumPressDuration = 1.0;

    [_imageV addGestureRecognizer:longPress];

}

-(void)longPress:(UILongPressGestureRecognizer *)press{

    NSLog(@"长按");

    //手势的声明周期(begin -- change --ended)

    if (press.state ==UIGestureRecognizerStateBegan || press.state ==UIGestureRecognizerStateChanged) {

       _imageV.image = [UIImageimageNamed:@"blue"];

    }

}

-(void)createPanGesture{

    //UIPanGestureRecognizer滑动

    UIPanGestureRecognizer *pan =[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panView:)];

   

    [_imageV addGestureRecognizer:pan];

}

-(void)panView:(UIPanGestureRecognizer *)pan{

    NSLog(@"滑动");

    //拿到滑动的偏移量

    CGPoint point1 = [pantranslationInView:_imageV];

    _imageV.center = CGPointMake(_imageV.center.x+point1.x,_imageV.center.y+point1.y);

    //重置_imageV滑动时候的偏移量,否则会叠加

    [pan setTranslation:CGPointZeroinView:_imageV];

}

-(void)createPinchGesture{

    //UIPinchGestureRecognizer缩放手势

    UIPinchGestureRecognizer *pinch =[[UIPinchGestureRecognizer alloc]initWithTarget:selfaction:@selector(pinchView:)];

    pinch.delegate = self;

    [_imageV addGestureRecognizer:pinch];

}

-(void)pinchView:(UIPinchGestureRecognizer *)pinch{

    //CGAffineTransformScale缩放

    //x,y同时缩放

    _imageV.transform =CGAffineTransformScale(_imageV.transform, pinch.scale, pinch.scale);

//    CGAffineTransformMakeScale(<#CGFloatsx#>, <#CGFloat sy#>)

    //pinch.scale缩放的倍数

    //重置手势的缩放倍数

    pinch.scale = 1.0f;

}

-(void)createRotationGesture{

    //UIRotationGestureRecognizer 旋转

    UIRotationGestureRecognizer *rotation =[[UIRotationGestureRecognizer alloc]initWithTarget:selfaction:@selector(rotationView:)];

    rotation.delegate = self;

    [_imageV addGestureRecognizer:rotation];

}

- (void)rotationView:(UIRotationGestureRecognizer*)rotation{

    //CGAffineTransformRotate旋转

    _imageV.transform=CGAffineTransformRotate(_imageV.transform, rotation.rotation);

    //rotation.rotation 旋转角度

    //重置旋转角度

    rotation.rotation = 0;

}

- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizershouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer{

    //如果两个手势在同一个试图上,return YES允许添加多个手势

    if (gestureRecognizer.view ==otherGestureRecognizer.view) {

        return YES;

    }

    //默认值是NO

    return NO;

}

-(void)createSwipGesture{

    //UISwipeGestureRecognizer轻扫

    UISwipeGestureRecognizer * swipe =[[UISwipeGestureRecognizer alloc]initWithTarget:selfaction:@selector(swipeView:)];

    //方向

    swipe.direction =UISwipeGestureRecognizerDirectionUp ;

    /*

     UISwipeGestureRecognizerDirection) {

     UISwipeGestureRecognizerDirectionRight = 1<< 0,

     UISwipeGestureRecognizerDirectionLeft  = 1 << 1,

     UISwipeGestureRecognizerDirectionUp    = 1 << 2,

     UISwipeGestureRecognizerDirectionDown  = 1 << 3

     */

    [_imageV addGestureRecognizer:swipe];

}

-(void)swipeView:(UISwipeGestureRecognizer *)swipe{

    NSLog(@"轻扫");

    _imageV.center = CGPointMake(_imageV.center.x, _imageV.center.y - 20);

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值