ios button长按手势势不执行怎么回事 ios

[IOS]241.03 滑动、拖动、长按手势_土豆_高清视频在线观看iOS 利用长按手势移动 Table View Cells_ios_ThinkSAAS
iOS 利用长按手势移动 Table View Cells
iOS 利用长按手势移动 Table View Cells
内容来源: 网络
本文译自:
目录:
你需要什么?
如何做?
如何将其利用至UICollectionView上?
何去何从?
本次的 cookbook-style 教程中介绍如何通过长按手势来移动 table view中的cell,这种操作方式就像苹果自家的天气 App 一样。
你可以直接把本文中的到吗添加到你的工程中,或者将其添加到我为你创建好的
中,也可以下载本文的。
你需要什么?
UILongGestureRecognizer
UITableView (可以用 UICollectionView 替代之)
UITableViewController (可以用 UIViewController 或 UICollectionViewController 替代之)
5 分钟。
如何做?
首先给 table view 添加一个 UILongGestureRecognizer。可以在 table view controller 的 viewDidLoad 方法中添加。
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(longPressGestureRecognized:)];
[self.tableView addGestureRecognizer:longPress];
记者为 gesture recognizer 添加 action 方法。该方法首先应该获取到在 table view 中长按的位置,然后找出这个位置对应的 cell 的 index。记住:这里获取到的 index path 有可能为 nil(例如,如果用户长按在 table view的section header上)。
- (IBAction)longPressGestureRecognized:(id)sender {
UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)
UIGestureRecognizerState state = longPress.
CGPoint location = [longPress locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
接着你需要处理UIGestureRecognizerStateBegan分支。如果获取到一个有效的 index path(non-nil),就去获取对应的 UITableViewCell,并利用一个 helper 方法获取这个 table view cell 的 snapshot view。然后将这个 snapshot view 添加到 table view 中,并将其 center 到对应的 cell上。
为了更好的用户体验,以及更自然的效果,在这里我把原始 cell 的背景设置为黑色,并给 snapshot view 增加淡入效果,让 snapshot view 比 原始 cell 稍微大一点,将它的Y坐标偏移量与手势的位置的Y轴对齐。这样处理之后,cell 就像从 table view 中跳出,然后浮在上面,并捕捉到用户的手指。
static UIView *snapshot =
static NSIndexPath *sourceIndexPath =
switch (state) {
case UIGestureRecognizerStateBegan: {
if (indexPath) {
sourceIndexPath = indexP
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
snapshot = [self customSnapshotFromView:cell];
__block CGPoint center = cell.
snapshot.center =
snapshot.alpha = 0.0;
[self.tableView addSubview:snapshot];
[UIView animateWithDuration:0.25 animations:^{
center.y = location.y;
snapshot.center =
snapshot.transform = CGAffineTransformMakeScale(1.05, 1.05);
snapshot.alpha = 0.98;
cell.backgroundColor = [UIColor blackColor];
} completion:nil];
将下面的方法添加到 .m 文件的尾部。该方法会根据传入的 view,返回一个对应的 snapshot view。
- (UIView *)customSnapshotFromView:(UIView *)inputView {
UIView *snapshot = [inputView snapshotViewAfterScreenUpdates:YES];
snapshot.layer.masksToBounds = NO;
snapshot.layer.cornerRadius = 0.0;
snapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0);
snapshot.layer.shadowRadius = 5.0;
snapshot.layer.shadowOpacity = 0.4;
当手势移动的时候,也就是UIGestureRecognizerStateChanged分支,此时需要移动 snapshot view(只需要设置它的 Y 轴偏移量即可)。如果手势移动的距离对应到另外一个 index path,就需要告诉 table view,让其移动 rows。同时,你需要对 data source 进行更新:
case UIGestureRecognizerStateChanged: {
CGPoint center = snapshot.
center.y = location.y;
snapshot.center =
if (indexPath && ![indexPath isEqual:sourceIndexPath]) {
[self.objects exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row];
[self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath];
sourceIndexPath = indexP
最后,当手势结束或者取消时,table view 和 data source 都是最新的。你所需要做的事情就是将 snapshot view 从 table view 中移除,并把 cell 的背景色还原为白色。
为了提升用户体验,我们将 snapshot view 淡出,并让其尺寸变小至与 cell 一样。这样看起来就像把 cell 放回原处一样。
default: {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:sourceIndexPath];
[UIView animateWithDuration:0.25 animations:^{
snapshot.center = cell.
snapshot.transform = CGAffineTransformI
snapshot.alpha = 0.0;
cell.backgroundColor = [UIColor whiteColor];
} completion:^(BOOL finished) {
[snapshot removeFromSuperview];
snapshot = 
sourceIndexPath = 
就这样,搞定了!编译并运行程序,现在可以通过长按手势对 tableview cells重新排序!
你可以在 GitHub 上下载到。
如何将其利用至UICollectionView上?
假设你已经有一个示例工程使用了 UICollectionView,那么你可以很简单的就使用上本文之前介绍的代码。所需要做的事情就是用 self.collectionView替换掉 self.tableView,并更新一下获取和移动 UICollectionViewCell 的调用方法。
这里有个练习,从 GitHub上 checkout 出 ,然后将 tap-和-hold 手势添加进去以对 cells 进行重排。这里可以下载到已经实现好了。
PHP开发框架
开发工具/编程工具
服务器环境
ThinkSAAS商业授权:
ThinkSAAS为用户提供有偿个性定制开发服务
ThinkSAAS将为商业授权用户提供二次开发指导和技术支持
让ThinkSAAS更好,把建议拿来。
开发客服微信iOS长按手势调用两次 - 简书
iOS长按手势调用两次
由于以前没有很细致的研究过长按手势,所以今天使用的时候发现长按手势会调用两次响应事件。
主要原因是长按手势会分别在UIGestureRecognizerStateBegan和UIGestureRecognizerStateEnded状态时调用响应函数
这时就需要在响应事件中增加手势状态的判断,根据具体的应用情况在相应的状态中执行操作。
typedefNS_ENUM(NSInteger, UIGestureRecognizerState) {
UIGestureRecognizerStatePossible,// the recognizer has not yet recognized its gesture, but may be evaluating touch events. this is the default state
UIGestureRecognizerStateBegan,// the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop
UIGestureRecognizerStateChanged,// the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop
UIGestureRecognizerStateEnded,// the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
UIGestureRecognizerStateCancelled,// the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible
UIGestureRecognizerStateFailed,// the recognizer has received a touch sequence that can not be recognized as the gesture. the action method will not be called and the recognizer will be reset to UIGestureRecognizerStatePossible
// Discrete Gestures – gesture recognizers that recognize a discrete event but do not report changes (for example, a tap) do not transition through the Began and Changed states and can not fail or be cancelled
UIGestureRecognizerStateRecognized =UIGestureRecognizerStateEnded// the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to
UIGestureRecognizerStatePossible
if (longPressGesture.state == UIGestureRecognizerStateBegan) {
// do something
}else if (longPressGesture.state == UIGestureRecognizerStateEnded){
// do something
版权声明:出自的原创作品 ,转载时必须注明出处及相应链接!
iOS开发者公会-技术1群 QQ群号:
iOS开发者...首先新建一个基于Sigle view Application的项目,名为GestureT我的项目结构如下:
往viewController.xib文件里拖动一个imageView,并使覆盖整个屏幕,改动属性为:
viewController.h文件:
#import &UIKit/UIKit.h&
@interface ViewController : UIViewController{
IBOutlet UIImageView *imageV
@property (nonatomic,retain)IBOutlet UIImageView *imageV
并使xib文件里的imageView与之连接;
然后是viewController.m文件的实现部分:
@synthesize imageV
CGFloat lastScaleFactor=1;//放大、缩小
CGFloat netR//旋转
CGPoint netT//平衡
NSArray *//图片数组
int imageIndex=0;//数组下标
- (void)viewDidLoad
//1、创建手势实例,并连接方法handleTapGesture,点击手势
UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];
//设置手势点击数,双击:点2下
tapGesture.numberOfTapsRequired=2;
// imageView添加手势识别
[imageView addGestureRecognizer:tapGesture];
//释放内存
[tapGesture release];
//2、手势为捏的姿势:按住option按钮配合鼠标来做这个动作在虚拟器上
UIPinchGestureRecognizer *pinchGesture=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinchGesture:)];
[imageView addGestureRecognizer:pinchGesture];//imageView添加手势识别
[pinchGesture release];
//3、旋转手势:按住option按钮配合鼠标来做这个动作在虚拟器上
UIRotationGestureRecognizer *rotateGesture=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotateGesture:)];
[imageView addGestureRecognizer:rotateGesture];
[rotateGesture release];
//4、拖手势
UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)];
// [imageView addGestureRecognizer:panGesture];
[panGesture release];
//5、划动手势
images=[[NSArray alloc]initWithObjects:@"cell.jpg",@"heihua.jpg",@"xuanyi.jpg", nil];
UISwipeGestureRecognizer *swipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];
[imageView addGestureRecognizer:swipeGesture];
[swipeGesture release];
UISwipeGestureRecognizer *swipeLeftGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];
swipeGesture.direction=UISwipeGestureRecognizerDirectionL//不设置黑夜是右
[imageView addGestureRecognizer:swipeLeftGesture];
[swipeLeftGesture release];
//6、长按手势
UILongPressGestureRecognizer *longpressGesutre=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongpressGesture:)];
//长按时间为1秒
longpressGesutre.minimumPressDuration=1;
//允许15秒中运动
longpressGesutre.allowableMovement=15;
//所需触摸1次
longpressGesutre.numberOfTouchesRequired=1;
[imageView addGestureRecognizer:longpressGesutre];
[longpressGesutre release];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//双击屏幕时会调用此方法,放大和缩小图片
-(IBAction)handleTapGesture:(UIGestureRecognizer*)sender{
//判断imageView的内容模式是否是UIViewContentModeScaleAspectFit,该模式是原比例,按照图片原时比例显示大小
if(sender.view.contentMode==UIViewContentModeScaleAspectFit){
//把imageView模式改成UIViewContentModeCenter,按照图片原先的大小显示中心的一部分在imageView
sender.view.contentMode=UIViewContentModeC
sender.view.contentMode=UIViewContentModeScaleAspectF
//捏的手势,使图片放大和缩小,捏的动作是一个连续的动作
-(IBAction)handlePinchGesture:(UIGestureRecognizer*)sender{
//得到sender捏手势的大小
CGFloat factor=[(UIPinchGestureRecognizer*)sender scale];
if(factor&1){
//图片放大
sender.view.transform=CGAffineTransformMakeScale(lastScaleFactor+(factor-1), (lastScaleFactor+(factor-1)));
sender.view.transform=CGAffineTransformMakeScale(lastScaleFactor*factor, lastScaleFactor*factor);
//状态是否结束,如果结束保存数据
if(sender.state==UIGestureRecognizerStateEnded){
if(factor&1){
lastScaleFactor+=(factor-1);
lastScaleFactor*=
//旋转手势
-(IBAction)handleRotateGesture:(UIGestureRecognizer*)sender{
//浮点类型,得到sender的旋转度数
CGFloat rotation=[(UIRotationGestureRecognizer*)sender rotation];
//旋转角度CGAffineTransformMakeRotation
CGAffineTransform transform=CGAffineTransformMakeRotation(rotation+netRotation);
//改变图像角度
sender.view.transform=
//状态结束,保存数据
if(sender.state==UIGestureRecognizerStateEnded){
netRotation+=
-(IBAction)handlePanGesture:(UIGestureRecognizer*)sender{
//得到拖的过程中的xy坐标
CGPoint translation=[(UIPanGestureRecognizer*)sender translationInView:imageView];
//平移图片CGAffineTransformMakeTranslation
sender.view.transform=CGAffineTransformMakeTranslation(netTranslation.x+translation.x, netTranslation.y+translation.y);
//状态结束,保存数据
if(sender.state==UIGestureRecognizerStateEnded){
netTranslation.x+=translation.x;
netTranslation.y+=translation.y;
//划动手势
-(IBAction)handleSwipeGesture:(UIGestureRecognizer*)sender{
//划动的方向
UISwipeGestureRecognizerDirection direction=[(UISwipeGestureRecognizer*) sender direction];
//判断是上下左右
switch (direction) {
case UISwipeGestureRecognizerDirectionUp:
NSLog(@"up");
case UISwipeGestureRecognizerDirectionDown:
NSLog(@"down");
case UISwipeGestureRecognizerDirectionLeft:
NSLog(@"left");
imageIndex++;//下标++
case UISwipeGestureRecognizerDirectionRight:
NSLog(@"right");
imageIndex--;//下标--
//得到不越界不&0的下标
imageIndex=(imageIndex&0)?([images count]-1):imageIndex%[images count];
//imageView显示图片
imageView.image=[UIImage imageNamed:[images objectAtIndex:imageIndex]];
//长按手势
-(IBAction)handleLongpressGesture:(UIGestureRecognizer*)sender{
//创建警告
UIActionSheet *actionSheet=[[UIActionSheet alloc]initWithTitle:@"Image options" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Save Image",@"Copy", nil];
//当前view显示警告
[actionSheet showInView:self.view];
[actionSheet release];
-(void)dealloc{
[images release];
[imageView release];
[super dealloc];
转载自:http://blog.csdn.net/chang6520/article/details/7924313
本文来自麦芒实验室,转载请注明出处,谢谢合作。}

我要回帖

更多关于 ios 长按手势 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信