• iOS手势的综合运用


    //自定义一个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

  • 相关阅读:
    设计模式学习笔记(三)简单工厂、工厂方法和抽象工厂之间的区别
    设计模式学习笔记(五)建造者模式和其他对象创建方式的区别
    设计模式学习笔记(二)工厂模式、模板模式和策略模式的混合使用
    域委派攻击详解
    测试人生 | 从功能到外企测开,工作1年半拿下年薪30万的测开 offer,这个95后小姐姐未来可期~
    测试人生 | 从外行到外包,从手工测试到知名互联大厂测开 这个90后小姐姐是怎么腾飞的?
    测试人生 | 从传统行业到名企大厂,薪资翻倍,我做到了
    测试人生 | 二线城市涨薪近10万 ,还能955,这样的机会你想不想要?
    测试人生 | 为了娃的奶粉钱,测试媛妈妈拿出考研的拼劲,半年终圆大厂梦!
    兑现 Service Mesh 的新价值:精确控制“爆炸半径”
  • 原文地址:https://www.cnblogs.com/gcb999/p/3189137.html
Copyright © 2020-2023  润新知