Permalink
Browse files

fix identifying touched views and their indices

  • Loading branch information...
1 parent e4137e0 commit 6f2e9577ef0cc513fd023b99a0909af3071484e8 norsez committed Jul 10, 2012
@@ -86,5 +86,11 @@ typedef NSUInteger BDDynamicGridCellLayoutStyle;
*/
@property (nonatomic, strong) BDRowInfo* rowInfo;
+/**
+ The view that grid views are contained in.
+ */
+@property (nonatomic, strong, readonly) UIView* gridContainerView;
+
+
@end
@@ -36,16 +36,17 @@
#import "BDDynamicGridCell.h"
-
-
+@interface BDDynamicGridCell(){
+ UIView * _gridContainerView;
+}
+@end
@implementation BDDynamicGridCell
- (id)init
{
self = [self initWithStyle:0 reuseIdentifier:@"GridCell"];
if (self) {
-
}
return self;
}
@@ -54,6 +55,10 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
{
self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
if (self) {
+ _gridContainerView = [[UIView alloc] initWithFrame:CGRectZero];
+ _gridContainerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
+ [self.contentView addSubview:_gridContainerView];
+
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
@@ -81,19 +86,22 @@ - (void)layoutSubviews{
- (void)layoutSubviewsAnimated:(BOOL)animated
{
[super layoutSubviews];
+ _gridContainerView.frame = CGRectMake(0, 0,
+ self.contentView.frame.size.width,
+ self.contentView.frame.size.height);
NSArray * oldFrames = [NSArray array];
if (animated) {
- for (int i=0; i<self.contentView.subviews.count; i++){
- UIView* subview = [self.contentView.subviews objectAtIndex:i];
+ for (int i=0; i<_gridContainerView.subviews.count; i++){
+ UIView* subview = [_gridContainerView.subviews objectAtIndex:i];
oldFrames = [oldFrames arrayByAddingObject:[NSValue valueWithCGRect:subview.frame]];
}
}
//layout what's in the cell
CGFloat aRowHeight = self.frame.size.height;
CGFloat totalWidth = 0;
- for (int i=0; i<self.contentView.subviews.count; i++){
- UIView* subview = [self.contentView.subviews objectAtIndex:i];
+ for (int i=0; i<_gridContainerView.subviews.count; i++){
+ UIView* subview = [_gridContainerView.subviews objectAtIndex:i];
//assume that for UIImageView, the size we want is the image size
if ([subview isKindOfClass:[UIImageView class]]){
UIImageView *iv = (UIImageView*)subview;
@@ -104,12 +112,12 @@ - (void)layoutSubviewsAnimated:(BOOL)animated
totalWidth = totalWidth + subview.frame.size.width;
}
- CGFloat widthScaling = ((self.contentView.frame.size.width - ((self.contentView.subviews.count+1) * self.viewBorderWidth ))/totalWidth);
+ CGFloat widthScaling = ((_gridContainerView.frame.size.width - ((_gridContainerView.subviews.count+1) * self.viewBorderWidth ))/totalWidth);
CGFloat accumWidth = self.viewBorderWidth;
//UIView* lastView;
NSArray *newFrames = [NSArray array];
- for (int i=0; i<self.contentView.subviews.count; i++){
- UIView* subview = [self.contentView.subviews objectAtIndex:i];
+ for (int i=0; i<_gridContainerView.subviews.count; i++){
+ UIView* subview = [_gridContainerView.subviews objectAtIndex:i];
CGRect newFrame = subview.frame;
newFrame = CGRectMake(0, 0, newFrame.size.width * widthScaling, aRowHeight - self.viewBorderWidth);
CGFloat leftMargin = i==0?0:(self.viewBorderWidth);
@@ -120,21 +128,21 @@ - (void)layoutSubviewsAnimated:(BOOL)animated
}
if (!animated) {
- for (int i=0; i<self.contentView.subviews.count; i++){
- UIView* subview = [self.contentView.subviews objectAtIndex:i];
+ for (int i=0; i<_gridContainerView.subviews.count; i++){
+ UIView* subview = [_gridContainerView.subviews objectAtIndex:i];
NSValue* newFrame = [newFrames objectAtIndex:i];
subview.frame = [newFrame CGRectValue];
}
}else {
- for (int i=0; i<self.contentView.subviews.count; i++){
- UIView* subview = [self.contentView.subviews objectAtIndex:i];
+ for (int i=0; i<_gridContainerView.subviews.count; i++){
+ UIView* subview = [_gridContainerView.subviews objectAtIndex:i];
NSValue* oldFrame = [oldFrames objectAtIndex:i];
subview.frame = [oldFrame CGRectValue];
}
[UIView animateWithDuration:1.f
animations:^{
- for (int i=0; i<self.contentView.subviews.count; i++){
- UIView* subview = [self.contentView.subviews objectAtIndex:i];
+ for (int i=0; i<_gridContainerView.subviews.count; i++){
+ UIView* subview = [_gridContainerView.subviews objectAtIndex:i];
NSValue* newFrame = [newFrames objectAtIndex:i];
subview.frame = [newFrame CGRectValue];
}
@@ -146,15 +154,15 @@ -(void)setViews:(NSArray *)views
{
//remove all subviews.
if (views == nil || views.count == 0) {
- for (UIView* sb in self.contentView.subviews) {
+ for (UIView* sb in _gridContainerView.subviews) {
[sb removeFromSuperview];
}
return;
}
for(UIView * sv in views){
sv.contentMode = UIViewContentModeScaleAspectFill;
- [self.contentView addSubview:sv];
+ [_gridContainerView addSubview:sv];
}
[self setNeedsLayout];
@@ -164,4 +172,5 @@ -(void)setViews:(NSArray *)views
@synthesize layoutStyle=_layoutStyle;
@synthesize viewBorderWidth;
@synthesize rowInfo;
+@synthesize gridContainerView=_gridContainerView;
@end
@@ -202,7 +202,7 @@ - (UIView*)viewAtIndex:(NSUInteger)index
BDDynamicGridCell *cell = (BDDynamicGridCell*)[_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:rowInfo.order inSection:0]];
NSUInteger realIndex = index - rowInfo.accumulatedViews;
- view = [cell.contentView.subviews objectAtIndex:realIndex];
+ view = [cell.gridContainerView.subviews objectAtIndex:realIndex];
return view;
}
@@ -212,7 +212,7 @@ - (NSArray *)visibleViews
NSArray* cells = [_tableView visibleCells];
NSArray* visibleViews = [[NSArray alloc] init];
for (BDDynamicGridCell *cell in cells) {
- visibleViews = [visibleViews arrayByAddingObjectsFromArray:cell.contentView.subviews];
+ visibleViews = [visibleViews arrayByAddingObjectsFromArray:cell.gridContainerView.subviews];
}
return visibleViews;
}
@@ -229,17 +229,17 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(didLongPress:)];
longPress.numberOfTouchesRequired = 1;
- [cell.contentView addGestureRecognizer:longPress];
+ [cell.gridContainerView addGestureRecognizer:longPress];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didDoubleTap:)];
doubleTap.numberOfTapsRequired = 2;
doubleTap.delaysTouchesBegan = YES;
- [cell.contentView addGestureRecognizer:doubleTap];
+ [cell.gridContainerView addGestureRecognizer:doubleTap];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didSingleTap:)];
singleTap.numberOfTapsRequired = 1;
singleTap.delaysTouchesBegan = YES;
- [cell.contentView addGestureRecognizer:singleTap];
+ [cell.gridContainerView addGestureRecognizer:singleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
}
@@ -297,57 +297,42 @@ - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
- (void)gesture:(UIGestureRecognizer*)gesture view:(UIView**)view viewIndex:(NSInteger*)viewIndex
{
- if (gesture.state == UIGestureRecognizerStateEnded) {
- BDDynamicGridCell *cell = (BDDynamicGridCell*) [gesture.view superview];
-
- CGPoint tapPoint = [gesture locationInView:gesture.view];
- NSArray *viewsSortedByXDesc = [gesture.view.subviews sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
- UIView * v1 = obj1;
- UIView * v2 = obj2;
- return v1.frame.origin.x - v2.frame.origin.x;
- }];
-
- if (viewsSortedByXDesc.count == 1) {
- *view = [viewsSortedByXDesc objectAtIndex:0];
- *viewIndex = (cell.rowInfo.accumulatedViews);
- return;
- }
-
- UIView * tappedView = nil;
- NSUInteger index = 0;
- for (int i=1; i<viewsSortedByXDesc.count; i++) {
- UIView * prevView = [viewsSortedByXDesc objectAtIndex:i-1];
- UIView * curView = [viewsSortedByXDesc objectAtIndex:i];
- if (prevView.frame.origin.x < tapPoint.x &&
- tapPoint.x < curView.frame.origin.x) {
- tappedView = curView;
- index = i;
- }
- }
- if (tappedView==nil) {
- tappedView = [viewsSortedByXDesc objectAtIndex:0];
- index = viewsSortedByXDesc.count ;
+ BDDynamicGridCell *cell = (BDDynamicGridCell*) [gesture.view.superview superview];
+
+ CGPoint locationInGridContainer = [gesture locationInView:gesture.view];
+ for (int i=0; i < cell.gridContainerView.subviews.count; i++){
+ UIView *subview = [cell.gridContainerView.subviews objectAtIndex:i];
+ if(CGRectContainsPoint(subview.frame, locationInGridContainer)){
+ *view = subview;
+ *viewIndex = ((cell.rowInfo.accumulatedViews) + i );
+ break;
}
-
- index = index - 1;
-
- *view = tappedView;
- *viewIndex = ((cell.rowInfo.accumulatedViews) + index);
- }else if (gesture.state == UIGestureRecognizerStatePossible){
- //
}
}
- (void)didLongPress:(UILongPressGestureRecognizer*)longPress
{
- if (longPress.state == UIGestureRecognizerStateRecognized) {
- UIView *view = nil;
- NSInteger viewIndex = -1;
- [self gesture:longPress view:&view viewIndex:&viewIndex];
+
+ UIView *view = nil;
+ NSInteger viewIndex = -1;
+ [self gesture:longPress view:&view viewIndex:&viewIndex];
+
+
+ if (longPress.state == UIGestureRecognizerStateBegan) {
+ if ([self.delegate respondsToSelector:@selector(longPressDidBeginAtView:index:)]) {
+ [self.delegate longPressDidBeginAtView:view index:viewIndex];
+ }
+ }else if (longPress.state == UIGestureRecognizerStateRecognized) {
+
+ if ([self.delegate respondsToSelector:@selector(longPressDidEndAtView:index:)]) {
+ [self.delegate longPressDidEndAtView:view index:viewIndex];
+ }
+
if (self.onLongPress) {
self.onLongPress(view, viewIndex);
}
+
}
}
@@ -370,6 +355,7 @@ - (void)didSingleTap:(UITapGestureRecognizer*)singleTap
if (singleTap.state == UIGestureRecognizerStateRecognized) {
UIView *view = nil;
NSInteger viewIndex = -1;
+ //DLog(@"view %@, viewIndex %d", view, viewIndex);
[self gesture:singleTap view:&view viewIndex:&viewIndex];
if (self.onSingleTap) {
self.onSingleTap(view, viewIndex);
@@ -58,6 +58,15 @@
*/
- (NSUInteger)minimumViewsPerCell;
+/**
+ This method is called when long press is recognized.
+ */
+- (void)longPressDidBeginAtView:(UIView*)view index:(NSUInteger)index;
+/**
+ This method is called when long press ends and before the onLongPress block is executed.
+ */
+- (void)longPressDidEndAtView:(UIView*)view index:(NSUInteger)index;
+
/**
@name Scrolling events
@@ -83,4 +92,6 @@
*/
- (void) gridViewDidEndScrolling;
+
+
@end

0 comments on commit 6f2e957

Please sign in to comment.