如何在 iPhone 中查找何时触摸或捏住 tableView 单元格?

发布于 2024-12-24 18:24:44 字数 715 浏览 4 评论 0原文

我有一个 tableView 已完成填充视图。当我点击单元格 tableView:didSelectRowAtIndexPath: 时被触发,但是当我编写 TouchBegan:withEvent 来查找 tableView 的触摸和捏合事件时,它没有被触发。 Tableview 位于视图上方是未触发的原因,如果是,如何查找何时 tableView 或其特定单元格被触摸或捏住。


我找到了一种在表视图单元格中查找捏合或触摸的方法,声明 UIPinchGestureRecognizer tableView中第一个对象:didSelectRowAtIndexPath: 即

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
[tblAccountsList addGestureRecognizer:pinch];
[pinch release];

然后在下面编写选择器方法...

- (void)handlePinch:(UIGestureRecognizer *)recognizer {
    NSLog(@"Pinch");
}

当我设置断点时它正在执行,但该方法被调用了 3 次。 如果有人找到解决方案,请分享...

提前致谢...

I have a tableView completed fill on view. when i tapped on a cell tableView:didSelectRowAtIndexPath: is getting fired, but when i wrote touchesBegan:withEvent to find touch and Pinch events of a tableView it was not fired. Tableview is above the view is that the reason of not firing, if yes how to find when tableView or its certain cell is touched or pinched.


I found a way to find Pinch Or touch in a Table View cell, declare UIPinchGestureRecognizer
object first in tableView:didSelectRowAtIndexPath:
i.e

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
[tblAccountsList addGestureRecognizer:pinch];
[pinch release];

then write the selector method below...

- (void)handlePinch:(UIGestureRecognizer *)recognizer {
    NSLog(@"Pinch");
}

when i set breakpoint it is executing, but the method was getting called for 3 times.
If any one find the solution share it please...

Thanks in advance...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假装爱人 2024-12-31 18:24:44

TouchBegan 是 UIView 和 UITableViewCell 方法,而不是 UIViewController 和 UIViewController 方法。 UITableViewController 方法。因此,您可以为 UITableViewCell 创建自定义类,它可以识别触摸事件和触摸委托,它正在为我工​​作。

  //TableViewCell.h

#import <UIKit/UIKit.h>

@class Util;

@interface TableViewCell : UITableViewCell {

}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;

@end
   //TableViewCell.m
#import "TableViewCell.h"
@implementation TableViewCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier      format:(NSString*)ec_format{

    if (self) {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

   }

   return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 {
//you receive touch here 
    NSLog(@"Category Touch %@",self.frame);


}

在表视图中您可以添加

- (void)viewDidLoad {

[super viewDidLoad];

// Add a pinch gesture recognizer to the table view.
UIPinchGestureRecognizer* pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
[self.tableView addGestureRecognizer:pinchRecognizer];

// Set up default values.
self.tableView.sectionHeaderHeight = HEADER_HEIGHT;
/*
 The section info array is thrown away in viewWillUnload, so it's OK to set the default values here. If you keep the section information etc. then set the default values in the designated initializer.
 */
rowHeight_ = DEFAULT_ROW_HEIGHT;
openSectionIndex_ = NSNotFound;
}


#pragma mark Handling pinches


-(void)handlePinch:(UIPinchGestureRecognizer*)pinchRecognizer {

/*
 There are different actions to take for the different states of the gesture recognizer.
 * In the Began state, use the pinch location to find the index path of the row with which the pinch is associated, and keep a reference to that in pinchedIndexPath. Then get the current height of that row, and store as the initial pinch height. Finally, update the scale for the pinched row.
 * In the Changed state, update the scale for the pinched row (identified by pinchedIndexPath).
 * In the Ended or Canceled state, set the pinchedIndexPath property to nil.
 */

if (pinchRecognizer.state == UIGestureRecognizerStateBegan) {

    CGPoint pinchLocation = [pinchRecognizer locationInView:self.tableView];
    NSIndexPath *newPinchedIndexPath = [self.tableView indexPathForRowAtPoint:pinchLocation];
    self.pinchedIndexPath = newPinchedIndexPath;

    SectionInfo *sectionInfo = [self.sectionInfoArray objectAtIndex:newPinchedIndexPath.section];
    self.initialPinchHeight = [[sectionInfo objectInRowHeightsAtIndex:newPinchedIndexPath.row] floatValue];
    // Alternatively, set initialPinchHeight = uniformRowHeight.

    [self updateForPinchScale:pinchRecognizer.scale atIndexPath:newPinchedIndexPath];
}
else {
    if (pinchRecognizer.state == UIGestureRecognizerStateChanged) {
        [self updateForPinchScale:pinchRecognizer.scale atIndexPath:self.pinchedIndexPath];
    }
    else if ((pinchRecognizer.state == UIGestureRecognizerStateCancelled) || (pinchRecognizer.state == UIGestureRecognizerStateEnded)) {
        self.pinchedIndexPath = nil;
    }
}
}


-(void)updateForPinchScale:(CGFloat)scale atIndexPath:(NSIndexPath*)indexPath {

if (indexPath && (indexPath.section != NSNotFound) && (indexPath.row != NSNotFound)) {

    CGFloat newHeight = round(MAX(self.initialPinchHeight * scale, DEFAULT_ROW_HEIGHT));

    SectionInfo *sectionInfo = [self.sectionInfoArray objectAtIndex:indexPath.section];
    [sectionInfo replaceObjectInRowHeightsAtIndex:indexPath.row withObject:[NSNumber numberWithFloat:newHeight]];
    // Alternatively, set uniformRowHeight = newHeight.

    /*
     Switch off animations during the row height resize, otherwise there is a lag before the user's action is seen.
     */
    BOOL animationsEnabled = [UIView areAnimationsEnabled];
    [UIView setAnimationsEnabled:NO];
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
    [UIView setAnimationsEnabled:animationsEnabled];
}
}

touchesBegan is a UIView and UITableViewCell method rather than a UIViewController & UITableViewController method. so you may create the the custom class for UITableViewCell it recognize the touch event and touch delegate it is working for me.

  //TableViewCell.h

#import <UIKit/UIKit.h>

@class Util;

@interface TableViewCell : UITableViewCell {

}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;

@end
   //TableViewCell.m
#import "TableViewCell.h"
@implementation TableViewCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier      format:(NSString*)ec_format{

    if (self) {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

   }

   return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 {
//you receive touch here 
    NSLog(@"Category Touch %@",self.frame);


}

In Table view you can add

- (void)viewDidLoad {

[super viewDidLoad];

// Add a pinch gesture recognizer to the table view.
UIPinchGestureRecognizer* pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
[self.tableView addGestureRecognizer:pinchRecognizer];

// Set up default values.
self.tableView.sectionHeaderHeight = HEADER_HEIGHT;
/*
 The section info array is thrown away in viewWillUnload, so it's OK to set the default values here. If you keep the section information etc. then set the default values in the designated initializer.
 */
rowHeight_ = DEFAULT_ROW_HEIGHT;
openSectionIndex_ = NSNotFound;
}


#pragma mark Handling pinches


-(void)handlePinch:(UIPinchGestureRecognizer*)pinchRecognizer {

/*
 There are different actions to take for the different states of the gesture recognizer.
 * In the Began state, use the pinch location to find the index path of the row with which the pinch is associated, and keep a reference to that in pinchedIndexPath. Then get the current height of that row, and store as the initial pinch height. Finally, update the scale for the pinched row.
 * In the Changed state, update the scale for the pinched row (identified by pinchedIndexPath).
 * In the Ended or Canceled state, set the pinchedIndexPath property to nil.
 */

if (pinchRecognizer.state == UIGestureRecognizerStateBegan) {

    CGPoint pinchLocation = [pinchRecognizer locationInView:self.tableView];
    NSIndexPath *newPinchedIndexPath = [self.tableView indexPathForRowAtPoint:pinchLocation];
    self.pinchedIndexPath = newPinchedIndexPath;

    SectionInfo *sectionInfo = [self.sectionInfoArray objectAtIndex:newPinchedIndexPath.section];
    self.initialPinchHeight = [[sectionInfo objectInRowHeightsAtIndex:newPinchedIndexPath.row] floatValue];
    // Alternatively, set initialPinchHeight = uniformRowHeight.

    [self updateForPinchScale:pinchRecognizer.scale atIndexPath:newPinchedIndexPath];
}
else {
    if (pinchRecognizer.state == UIGestureRecognizerStateChanged) {
        [self updateForPinchScale:pinchRecognizer.scale atIndexPath:self.pinchedIndexPath];
    }
    else if ((pinchRecognizer.state == UIGestureRecognizerStateCancelled) || (pinchRecognizer.state == UIGestureRecognizerStateEnded)) {
        self.pinchedIndexPath = nil;
    }
}
}


-(void)updateForPinchScale:(CGFloat)scale atIndexPath:(NSIndexPath*)indexPath {

if (indexPath && (indexPath.section != NSNotFound) && (indexPath.row != NSNotFound)) {

    CGFloat newHeight = round(MAX(self.initialPinchHeight * scale, DEFAULT_ROW_HEIGHT));

    SectionInfo *sectionInfo = [self.sectionInfoArray objectAtIndex:indexPath.section];
    [sectionInfo replaceObjectInRowHeightsAtIndex:indexPath.row withObject:[NSNumber numberWithFloat:newHeight]];
    // Alternatively, set uniformRowHeight = newHeight.

    /*
     Switch off animations during the row height resize, otherwise there is a lag before the user's action is seen.
     */
    BOOL animationsEnabled = [UIView areAnimationsEnabled];
    [UIView setAnimationsEnabled:NO];
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
    [UIView setAnimationsEnabled:animationsEnabled];
}
}
樱娆 2024-12-31 18:24:44

它被调用了 3 次,因为每次手势识别器的状态发生变化时都会调用它,而不是每次识别手势时都会调用它:)

尝试此更改:

- (void)handlePinch:(UIGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateRecognized) {
        NSLog(@"Pinch");
    }
}

并查看 手势识别器这里,特别是state 属性。

It's being called 3 times because it's called each time the state of the gesture recogniser changes, not each time the gesture is recognised :)

Try this change :

- (void)handlePinch:(UIGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateRecognized) {
        NSLog(@"Pinch");
    }
}

And check out the docs for gesture recognisers here, specifically the state property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文