一、創建兩個view 視圖 LeftMenuView 、ContentView。動畫
二、建立 UIPanGestureRecognizer 監控手勢對象 並寫入手勢處理的方法atom
三、爲ContentView視圖添加 監控手勢的對象。spa
四、添加 自動動畫處理 方法 [UIViewanimateWithDuration:0.1fanimations:animatins];code
// // 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