代码改变世界

iOS手势的综合运用

2013-07-14 10:21  甘超波  阅读(336)  评论(0编辑  收藏  举报
//自定义一个VIEW封装手势功能
//  CustormView.m
//  gesterDemoo
//
//  Created by ganchaobo on 13-7-13.
//  Copyright (c) 2013年 ganchaobo. All rights reserved.
//

#import "CustormView.h"


@interface CustormView (){
    UIView *_parentview;
    CGPoint _lastCenter;
}

@end

@implementation CustormView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    // Initialization code
    }
    return self;
}

-(id)INitwithContetView:(UIView*)contentview ParentView:(UIView *)parentView{
    self=[super initWithFrame:contentview.bounds];
    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self addGestureRecognizer:tap];
    [self addGestureRecognizer:pan];
    [pan release];
    [tap release];
    _parentview=parentView;
    _lastCenter=self.center;
    return self;
    
}

-(void)pan:(UIPanGestureRecognizer *)pan{
    //移动点的位置
    CGPoint panPoint=[pan translationInView:_parentview];
    CGFloat x=pan.view.center.x+panPoint.x;
    NSLog(@"%f---->%f,--%f",panPoint.x,_lastCenter.x,x);
    if(x<_lastCenter.x){
        x=_lastCenter.x;
    }
  

    self.center=CGPointMake(x, _lastCenter.y);

    if(pan.state==UIGestureRecognizerStateEnded){
      
        [UIView animateWithDuration:0.75 delay:0.2 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            if(x>320){
                self.center=CGPointMake(390, _lastCenter.y);
            }
            else{
                self.center=_lastCenter;
            }
            
        } completion:^(BOOL finished) {
            
        }];
    }
    [pan setTranslation:CGPointZero inView:self];
}

-(void)tap:(UITapGestureRecognizer *)tap{
    NSLog(@"tap");
    [UIView animateWithDuration:0.75 delay:0.2 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.center=_lastCenter;
    } completion:^(BOOL finished) {
        
    }];
}


@end
//
//  CustormView.m
//  gesterDemoo
//
//  Created by ganchaobo on 13-7-13.
//  Copyright (c) 2013年 ganchaobo. All rights reserved.
//

#import "CustormView.h"


@interface CustormView (){
    UIView *_parentview;
    CGPoint _lastCenter;
}

@end

@implementation CustormView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    // Initialization code
    }
    return self;
}

-(id)INitwithContetView:(UIView*)contentview ParentView:(UIView *)parentView{
    self=[super initWithFrame:contentview.bounds];
    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self addGestureRecognizer:tap];
    [self addGestureRecognizer:pan];
    [pan release];
    [tap release];
    _parentview=parentView;
    _lastCenter=self.center;
    return self;
    
}

-(void)pan:(UIPanGestureRecognizer *)pan{
    //移动点的位置
    CGPoint panPoint=[pan translationInView:_parentview];
    CGFloat x=pan.view.center.x+panPoint.x;
    NSLog(@"%f---->%f,--%f",panPoint.x,_lastCenter.x,x);
    if(x<_lastCenter.x){
        x=_lastCenter.x;
    }
  

    self.center=CGPointMake(x, _lastCenter.y);

    if(pan.state==UIGestureRecognizerStateEnded){
      
        [UIView animateWithDuration:0.75 delay:0.2 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            if(x>320){
                self.center=CGPointMake(390, _lastCenter.y);
            }
            else{
                self.center=_lastCenter;
            }
            
        } completion:^(BOOL finished) {
            
        }];
    }
    [pan setTranslation:CGPointZero inView:self];
}

-(void)tap:(UITapGestureRecognizer *)tap{
    NSLog(@"tap");
    [UIView animateWithDuration:0.75 delay:0.2 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.center=_lastCenter;
    } completion:^(BOOL finished) {
        
    }];
}


@end
@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIView *firstview=[[UIView alloc] initWithFrame:self.view.bounds];
    firstview.backgroundColor=[UIColor redColor];
    [self.view addSubview:firstview];
    [firstview release];


    CustormView *view=[[CustormView alloc] INitwithContetView:self.view ParentView:self.view];
    view.backgroundColor=[UIColor yellowColor];
    [self.view addSubview:view];
    [view release];

    
    
}

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