代码改变世界

ios 中手势用法

2013-07-13 19:57  甘超波  阅读(710)  评论(0编辑  收藏  举报

pan拖动手势

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self Pan];
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark -拖动手势Pan
-(void)Pan{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(100, 50, 100, 100)];
    view.backgroundColor=[UIColor redColor];
    [self.view addSubview:view];
    [view release];
    
    UIPanGestureRecognizer *pin=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pin:)];
    [view addGestureRecognizer:pin];
    [pin release];
}

-(void)pin:(UIPanGestureRecognizer*)pin{
    //得到pin移动的位置,向左拖动是左拖动x为负数,向上拖动y为负数,向下拖动y为整数,向右边拖动为x为整数
    CGPoint point= [pin translationInView:self.view];
    NSLog(@"%@",NSStringFromCGPoint(point));
    //手势结束的状态
    if(pin.state==UIGestureRecognizerStateEnded){
        NSLog(@"手指已离开屏幕,手势结束");
    }
    
    //注意一定要加下面一句 ,清除手势为原来坐标
    [pin setTranslation:CGPointZero inView:self.view];
    
}

 缩放手势

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self Princh];
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark -Princh
-(void)Princh{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
    view.backgroundColor=[UIColor redColor];
    [self.view addSubview:view];
    UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    [view addGestureRecognizer:pinch];
    [pinch release];
    
    [view release];
    
}

-(void)pinch:(UIPinchGestureRecognizer *)pin{
    
    //主要属性
    CGFloat scale= pin.scale;
    pin.view.transform=CGAffineTransformScale(pin.view.transform, pin.scale, pin.scale);
    if(pin.state==UIGestureRecognizerStateEnded){
        
    }
    scale=1;
    
}

缩放

    [self Rotaion];
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark -Princh
-(void)Rotaion{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
    view.backgroundColor=[UIColor redColor];
    [self.view addSubview:view];
    UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rote:)];
    [view addGestureRecognizer:rotation];
    
    [view release];
    
}

-(void)rote:(UIRotationGestureRecognizer *)rotation{
    CGFloat rt=rotation.rotation;
    rotation.view.transform=CGAffineTransformRotate(rotation.view.transform, rt);
    if(rotation.state==UIGestureRecognizerStateEnded){
        
    }
    rt=0;
}

 Tap手势

-(void)Rotaion{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
    view.backgroundColor=[UIColor redColor];
    [self.view addSubview:view];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [view addGestureRecognizer:tap];
    [tap release];
    
}


-(void)tap:(UITapGestureRecognizer *)tap{
    tap.view.backgroundColor=[UIColor yellowColor];
}

 原文地址:http://blog.csdn.net/totogo2010/article/details/8615940