Use*_*892 3 uitableview uitouch pinch touchesbegan
我有一个tableView完成填充视图.当我点击一个单元格表时查看:didSelectRowAtIndexPath:被解雇了,但是当我写了touchesBegan:withEvent来查找tableView的触摸和Pinch事件时它没有被触发.Tableview上面的视图是没有触发的原因,如果是,当tableView或其某个单元格被触摸或收缩时如何查找.
我找到了一种在Table View单元格中找到Pinch或touch的方法,首先在tableView中声明UIPinchGestureRecognizer对象:didSelectRowAtIndexPath:ie
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
[tblAccountsList addGestureRecognizer:pinch];
[pinch release];
Run Code Online (Sandbox Code Playgroud)
然后写下面的选择器方法......
- (void)handlePinch:(UIGestureRecognizer *)recognizer {
NSLog(@"Pinch");
}
Run Code Online (Sandbox Code Playgroud)
当我设置断点时它正在执行,但该方法被调用了3次.如果有人发现解决方案请分享它...
提前致谢...
Bal*_*Mca 10
touchesBegan是一个UIView和UITableViewCell方法,而不是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);
}
Run Code Online (Sandbox Code Playgroud)
在表视图中,您可以添加
- (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];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5426 次 |
| 最近记录: |