IOS 实现边滑动边缩放的类似qq主界面的页面切换功能

来源:互联网 发布:comparebeyond4 mac 编辑:程序博客网 时间:2024/05/17 01:54

转自http://blog.csdn.net/djl4104804/article/details/45557191

原理:

调用UIView的三个delegate函数(主要正对触摸+滑动操作的回调):

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event


第一个函数用来记录滑动的起始点;

第二个函数,用来在滑动中,改变UIView的中心点位置,以及缩放UIView;

第三个函数,对于滑动结束后的处理,主要是未滑过分界线和分界线后,UIView自动滑动到预先设定位置。


TouchEaglView.h

[cpp] view plaincopy
  1. #import <UIKit/UIKit.h>  
  2.   
  3.   
  4. @interface TouchEaglView : UIView  
  5.   
  6.   
  7. @property (assign, nonatomic) CGPoint beginpoint;  
  8.   
  9. @property id delegate;  
  10.   
  11. @end  


下面是向右滑动缩小的UIView子类实现:

TouchEagleView.m

[cpp] view plaincopy
  1. //  
  2. //  TouchEaglView.m  
  3. //  moveView  
  4. //  
  5. //  Created by wangdan on 10/1/14.  
  6. //  Copyright (c) 2014 wangdan. All rights reserved.  
  7. //  
  8.   
  9. #import "TouchEaglView.h"  
  10.   
  11. @implementation TouchEaglView  
  12.   
  13.   
  14. - (id)initWithFrame:(CGRect)frame  
  15. {  
  16.   
  17.     self = [super initWithFrame:frame];  
  18.     if (self)  
  19.     {  
  20.         // Initialization code  
  21.     }  
  22.     return self;  
  23.       
  24.       
  25. }  
  26.   
  27.   
  28.   
  29. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
  30. {  
  31.       
  32.     UITouch *touch = [touches anyObject];  
  33.     self.beginpoint = [touch locationInView:self];  
  34.     [super touchesBegan:touches withEvent:event];  
  35.     [super bringSubviewToFront:self];  
  36.       
  37.       
  38. }  
  39.   
  40.   
  41.   
  42. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  
  43. {  
  44.   
  45.     CGPoint point = [[touches anyObject] locationInView:self];  
  46.     float dx = point.x - self.beginpoint.x;  
  47.     CGPoint newcenter = CGPointMake(self.center.x + dx, self.center.y);  
  48.       
  49.     if(self.beginpoint.x<10)  
  50.     {  
  51.         if(dx>0)  
  52.         {  
  53.               newcenter.x=MIN(self.bounds.size.width*5/4,newcenter.x);  
  54.               self.center=newcenter;  
  55.         }  
  56.         else  
  57.         {  
  58.             newcenter.x=MAX(self.superview.bounds.size.width/2,newcenter.x);  
  59.             self.center=newcenter;  
  60.         }  
  61.       
  62.         [UIView animateWithDuration:0.005 animations:^  
  63.          {  
  64.              self.transform = CGAffineTransformMakeScale(1-(newcenter.x-160)/1920, 1-(newcenter.x-160)/960);  
  65.          }completion:^(BOOL finish)  
  66.          {  
  67.                
  68.          }];  
  69.     }  
  70.     NSLog(@"begin size is %f",self.beginpoint.x);  
  71.       
  72. }  
  73.   
  74.   
  75.   
  76. // zhege shi chumo jieshu ,huamian zidong tan dao youbian  
  77. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event  
  78. {  
  79.     if(self.center.x>self.superview.bounds.size.width)  
  80.     {  
  81.          
  82.         [UIView animateWithDuration:0.2 animations:^  
  83.          {  
  84.             self.center=CGPointMake(self.bounds.size.width*5/4, self.center.y);  
  85.             self.transform = CGAffineTransformMakeScale(0.875, 0.75);  
  86.          }completion:^(BOOL finish)  
  87.          {  
  88.                
  89.          }];  
  90.           
  91.     }  
  92.     else  
  93.     {  
  94.           
  95.         [UIView animateWithDuration:0.2 animations:^  
  96.          {  
  97.              self.center=CGPointMake(self.bounds.size.width/2,self.center.y);  
  98.              self.transform = CGAffineTransformMakeScale(1, 1);  
  99.          }completion:^(BOOL finish)  
  100.          {  
  101.                
  102.          }];  
  103.     }  
  104.       
  105. }  
  106.   
  107.   
  108.   
  109.   
  110. @end  
下面是滑动前的TouchEagleView 显示


下面是滑动后的图片:



上图中左边绿色部分,会随着黄色TouchEagleView滑动而右移并且放大,需要单独实现,原理和上面代码类似


0 0
原创粉丝点击