作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 tableView 已完成填充 View 。当我点击单元格 tableView:didSelectRowAtIndexPath: 被触发时,但是当我编写 touchesBegan:withEvent 来查找 tableView 的触摸和 Pinch 事件时,它没有被触发。 Tableview 在 View 上方是不触发的原因,如果是,如何找到 tableView 或其某个单元格何时被触摸或捏合。
我找到了一种在表格 View 单元格中找到捏或触摸的方法,声明 UIPinchGestureRecognizer
tableView 中的第一个对象:didSelectRowAtIndexPath:
IE
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
[tblAccountsList addGestureRecognizer:pinch];
[pinch release];
- (void)handlePinch:(UIGestureRecognizer *)recognizer {
NSLog(@"Pinch");
}
最佳答案
touchesBegan 是 UIView 和 UITableViewCell 方法,而不是 UIViewController 和 UITableViewController 方法。因此,您可以为 UITableViewCell 创建自定义类,它可以识别触摸事件并为我工作的触摸委托(delegate)。
//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];
}
}
关于uitableview - 如何找到在 iphone 中触摸或捏住 tableView 单元格的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8742173/
我有一个应用程序可以跟踪走路的人。一旦获得用户标题,我就会根据标题旋转 MKAnnotationView。跟踪和旋转 MKAnnotationView 时一切正常,移动或捏合 map 时除外。当我捏合
我是一名优秀的程序员,十分优秀!