您的位置:首页 > 产品设计 > UI/UE

UI之摇晃手机和触摸手势

2015-11-15 15:12 381 查看
摇晃手机
-(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.c
4000
enter.x, _imageV.center.y - 20);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: