On a UITableView, I used Custom Cells with this code:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
gestureStartPoint = [touch locationInView:self.contentView];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self.contentView];
CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);
if(deltaX < kMinimumGestureLength && deltaY < kMaximumVariance&&!swiped){
//do something
NSLog(@"Tapped %d",indexRow);
//return YES;
} else if(deltaX < kMinimumGestureLength && deltaY < kMaximumVariance&&swiped) {
swiped = FALSE;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self.contentView];
CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);
if(deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance){
//do something
NSLog(@"Swiped %d",indexRow);
gestureStartPoint = [touch locationInView:self.contentView];
swiped = TRUE;
}
else if(deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance){
//do something
}
}
Before I added this, didSelectRowAtIndexPath was being called, however, now it is not. I can comment out the section and it works fine. Anyone know a way to fix this?
EDIT: Posting the working code
Customcell.h
#define kMinimumGestureLength 30
#define kMaximumVariance 5
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@interface CustomCell : UITableViewCell {
UILabel *primaryLabel;
UILabel *secondaryLabel;
UIImageView *myImageView;
CGPoint gestureStartPoint;
int indexRow;
BOOL swiped;
}
@property(nonatomic,retain)UILabel *primaryLabel;
@property(nonatomic,retain)UILabel *secondaryLabel;
@property(nonatomic,retain)UIImageView *myImageView;
@end
CustomCell.m
#import "CustomCell.h"
@implementation CustomCell
@synthesize primaryLabel,secondaryLabel,myImageView;
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame= CGRectMake(boundsX+10 ,0, 50, 50);
myImageView.frame = frame;
frame= CGRectMake(boundsX+70 ,5, 200, 25);
primaryLabel.frame = frame;
frame= CGRectMake(boundsX+70 ,30, 100, 15);
secondaryLabel.frame = frame;
}
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier indexd:(int)indexd {
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
// Initialization code
primaryLabel = [[UILabel alloc]init];
primaryLabel.textAlignment = UITextAlignmentLeft;
primaryLabel.font = [UIFont systemFontOfSize:14];
secondaryLabel = [[UILabel alloc]init];
secondaryLabel.textAlignment = UITextAlignmentLeft;
secondaryLabel.font = [UIFont systemFontOfSize:8];
myImageView = [[UIImageView alloc]init];
[self.contentView addSubview:primary开发者_Python百科Label];
[self.contentView addSubview:secondaryLabel];
[self.contentView addSubview:myImageView];
indexRow = indexd;
}
swiped = false;
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
gestureStartPoint = [touch locationInView:self.contentView];
// [super touchesbegan];
[super touchesBegan:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self.contentView];
CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);
if(deltaX < kMinimumGestureLength && deltaY < kMaximumVariance&&!swiped){
//do something
NSLog(@"Tapped %d",indexRow);
[super touchesEnded:touches withEvent:event];
//return YES;
} else if(deltaX < kMinimumGestureLength && deltaY < kMaximumVariance&&swiped) {
swiped = FALSE;
}
//[super touchesEnded];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self.contentView];
CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);
if(deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance){
//do something
NSLog(@"Swiped %d",indexRow);
gestureStartPoint = [touch locationInView:self.contentView];
swiped = TRUE;
//[self.superview.dataSource editCell];
}
else if(deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance){
//do something
}
//[super touchesMoved];
}
@end
I'm not 100% certain, but I think you need to call the super methods in order to continue to have the default behavior.
[super touchesBegan:...];
[super touchesEnded:...];
[super touchesMoved:...];
Each in it's own method.
This seems to be similar to this question. try calling the parent methods with [super touchesbegan]
et al.
精彩评论