//自定义一个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>){
self.center=CGPointMake(, _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>){
self.center=CGPointMake(, _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. ERROR 1300 (HY000): Invalid utf8 character string: &#39;&#39;
  2. SharePoint Server 2013开发之旅(二):使用在线的开发人员网站进行SharePoint App开发
  3. Scrum Meeting (Oct. 27 2014)
  4. CLR调试报错“Visual Studio远程调试监视器 (MSVSMON.EXE) 的 64 位版本无法调试 32 位进程或 32 位转储。请改用 32 位版本”的解决
  5. 夺命雷公狗---node.js---20之项目的构建在node+express+mongo的博客项目5mongodb在项目中实现添加数据
  6. 利用DetachedCriteria实现模糊查询和分页
  7. Laxcus大数据管理系统2.0(11)- 第九章 容错
  8. 关于System.Collections空间
  9. Hadoop MapReduce概念学习系列之mr程序详谈(二十三)
  10. cookie、localStorage、sessionStorage之间的区别
  11. C#用注册表开机自动启动某某软件
  12. IOC原理分析
  13. vmware 使用技巧
  14. iOS基础 - UIDatePicker and UIPickerView and UITextField
  15. cocos2d-x学习之路之工作吐槽
  16. linq给list集合数据分页
  17. HTML5 full-screen全屏API
  18. A1125. Chain the Ropes
  19. 使用jwt
  20. 〖Linux〗让Kubuntu的“启动栏”与Win7“任务栏”的界面和功能一样

热门文章

  1. [leetcode]Spiral Matrix II @ Python
  2. ASP.NET网站管理工具的【安全】功能无法使用问题
  3. mono touch登录设计
  4. TCP Socket Programming in Node.js
  5. 【转】Linux防火墙(iptables)之黑名单
  6. linux里tmpfs文件系统
  7. Javascript 面向对象实践
  8. Ext JS isField为空或不是对象问题的解决
  9. 数据驱动测试二:使用TestNG和CSV文件进行数据驱动
  10. conEmu的使用笔记