UIPanGestureRecognizer 监控手势练习

 

 

1、建立两个view 视图 LeftMenuView 、ContentView。

2、创建 UIPanGestureRecognizer  监控手势对象 并写入手势处理的方法

3、为ContentView视图添加 监控手势的对象。

4、添加 自动动画处理 方法 [UIViewanimateWithDuration:0.1fanimations:animatins];

 

 

//
//  MainController.m
//  LeftMenuSlither
//
//  Created by 王 强 on 13-8-6.
//  Copyright (c) 2013年 王 强. All rights reserved.
//

#import "MainController.h"

@interface MainController ()

@property (nonatomic,readonly) IBOutlet UIView * leftMenuView;
@property (nonatomic,readonly) IBOutlet UIView * contentView;

@property(nonatomic , retain) UIPanGestureRecognizer * pan;
@end

@implementation MainController
const int contentShrink  = 280;
float currentTranslate = 0;
@synthesize leftMenuView,contentView;
@synthesize pan;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    pan  =  [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(paninContentView:)];
    [self.contentView addGestureRecognizer:pan];
    
    // Do any additional setup after loading the view.
}
-(void) paninContentView : (UIPanGestureRecognizer *) sender
{
    CGFloat x = [sender translationInView:self.contentView].x;
    
    UIView * view;
    if (sender.state == UIGestureRecognizerStateChanged) {
        self.contentView.transform = CGAffineTransformMakeTranslation(x+currentTranslate, 0);
        view = self.leftMenuView;
        
    }else if (sender.state == UIGestureRecognizerStateEnded){
        if(x > 10)
        {
            currentTranslate = contentShrink;
            [self moveContent:NO];
        }else{
            currentTranslate = 0;
            [self moveContent:YES];

        }
    }

}
-(void) moveContent :(BOOL) type
{
    void (^animatins)(void) = ^{
        if(type){
        
            self.contentView.transform = CGAffineTransformMakeTranslation(0, 0);
            
        }else{
            self.contentView.transform = CGAffineTransformMakeTranslation(contentShrink, 0);
        
        }
    
    };
    
    [UIView animateWithDuration:0.1f animations:animatins];
   // [UIView animateWithDuration:duration animations:animations completion:complete];


}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)bcolor:(UIButton *) sender
{
    int tag = sender.tag;
    UIColor * c;
    switch (tag) {
        case 1:
            c = [UIColor redColor];
            break;
        case 2:
            c = [UIColor blueColor];
            break;
        case 3:
            c = [UIColor grayColor];
            break;
            
        default:
            break;
    }
    currentTranslate = 0;
    [UIView animateWithDuration:1 animations:^{
        self.contentView.transform = CGAffineTransformMakeTranslation(0, 0);
        self.contentView.backgroundColor = c;

    }];
}
@end

 

posted @ 2013-08-06 11:10  真是猿粪啊  阅读(383)  评论(0编辑  收藏  举报