2013-08-07 45 views
1

我在scrollView中有一些通用生成的ImageViews這些圖像中的每一個都有2個gestureRecognizer用於ImageView上的單/雙擊。現在我的問題是如何識別是第一次還是第二次使用ImageView。在某些場景中,使用ImageView的標籤很容易,但每個ImageView上都有兩個不同的gestureRecognizer,每個gestureRecognizer都使用基於標籤號的不同識別方法來識別圖像。識別沒有標記的UIImageView

在這裏,我動態生成ImageViews:

-(void) initLevels{ 
_level = [Level alloc]; 
_unit = [Unit alloc]; 
self->_units = [[NSMutableArray alloc] init]; 
_keys = [[NSMutableArray alloc] init] 
; 
int x = 0; 
int y = 0; 
int i = 0; 

for (NSObject *object in self->_levels) { 
    if ([object isKindOfClass:_level.class] && i != 0) { 
     x = x + MARGIN_RIGHT + OBJECT_WIDTH; 
     y = 0; 
     } 
    else if ([object isKindOfClass:_unit.class]){ 
     _unit = (Unit *) object; 
     [self->_units addObject:_unit.description]; 
     UIImageView *imageView = [[UIImageView alloc] initWithImage:self.box]; 
     [imageView setFrame:CGRectMake(x, y, OBJECT_WIDTH, BOX_HEIGHT)]; 
     imageView.highlighted = TRUE; 
     imageView.tag = i; //when this is not outlined the gestureRecognizer for singleTapping works but on the other hand the double tap gestureRecognizer just works for the first object, because its' tag is set on 0. 

     UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(unitDoubleTapped:)]; 
     doubleTapGesture.numberOfTapsRequired = 2; 
     imageView.userInteractionEnabled = YES; 
     [imageView addGestureRecognizer:doubleTapGesture]; 

     UITapGestureRecognizer *singleTapGesture = 
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(unitSingleTapped:)]; 
     singleTapGesture.numberOfTapsRequired = 1; 

    //telling the singleTapGesture to fail the doubleTapGesture, so both doesn't fire at the same time 
     [singleTapGesture requireGestureRecognizerToFail:doubleTapGesture]; 
     [imageView addGestureRecognizer:singleTapGesture]; 
     UILabel *labelHeadline = [[UILabel alloc] initWithFrame:CGRectMake(5, 2, 220, 20)]; 
     [labelHeadline setFont:[UIFont boldSystemFontOfSize:12]]; 
     labelHeadline.textAlignment = NSTextAlignmentCenter; 
     [labelHeadline setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0]]; 

     labelHeadline.text = _unit.headline; 
     labelHeadline.numberOfLines = 0; 
     [labelHeadline sizeToFit]; 

     UILabel *labelPrice = [LabelUtils deepLabelCopy:labelHeadline withText:[NSString stringWithFormat:@"Price: %@",_unit.price] withFrame:NO]; 
     [labelPrice setTextAlignment:NSTextAlignmentLeft]; 
     [labelPrice setFrame:CGRectMake(labelHeadline.frame.origin.x, labelHeadline.frame.origin.y + labelHeadline.frame.size.height + 2, 220, 20)]; 

     UILabel *labelCRM = [LabelUtils deepLabelCopy:labelHeadline withText:[NSString stringWithFormat:@"CRM: %@", _unit.crm] withFrame:NO]; 
     [labelCRM setTextAlignment:NSTextAlignmentLeft]; 

     [labelCRM setFrame:CGRectMake(labelPrice.frame.origin.x, labelPrice.frame.origin.y + labelPrice.frame.size.height + 2, 220, 20)]; 



     UITextView *textView= [[UITextView alloc] initWithFrame:CGRectMake(0,0, OBJECT_WIDTH, OBJECT_HEIGHT)]; 
     [textView addSubview:labelHeadline]; 
     [textView addSubview:labelPrice]; 
     [textView addSubview:labelCRM]; 
     [textView setUserInteractionEnabled:NO]; 
     [textView setEditable:NO]; 
     textView.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0]; 
     textView.textAlignment = NSTextAlignmentLeft; 
     [imageView addSubview:textView]; 
     [_scrollView addSubview:imageView]; 

     y = y + MARGIN_BOTTOM + BOX_HEIGHT; 
    } 
    [self->_keys addObject:[NSNumber numberWithInt:i]]; 
    i++; 
}//remove the last keys which are to much in the _keys array 
while ([self->_keys count] > ([self->_units count])) { 
    [_keys removeLastObject]; 
    i--; 
} 

self.contents = [NSDictionary dictionaryWithObjects:self->_units forKeys:_keys]; 
} 

下面是兩個手勢識別

-(void)unitDoubleTapped:(UIGestureRecognizer *)gestureRecognizer{ 
self->_unitViewForDoubleTapIdentification = (UIImageView *)gestureRecognizer.view; 
switch (self->_unitViewForDoubleTapIdentification.tag) { 
    case 0: 
     [_unitViewForDoubleTapIdentification setHighlightedImage:self.transparentBox]; 
     self->_unitViewForDoubleTapIdentification.tag = 1; 
     break; 
    case 1: 
     [_unitViewForDoubleTapIdentification setHighlightedImage:self.box]; 
     self->_unitViewForDoubleTapIdentification.tag = 0; 
     break; 
    default: 
     break; 
} 

} 

,並在這裏singleTap

- (IBAction)unitSingleTapped:(id)sender { 
[self dismissAllPopTipViews]; 
UIGestureRecognizer *gestureRecognizer = [UIGestureRecognizer alloc]; 
gestureRecognizer = (UIGestureRecognizer *)sender; 
UIImageView *imageView = [[UIImageView alloc] init]; 
imageView = (UIImageView *)gestureRecognizer.view; 

if (sender == _currentPopTipViewTarget) { 
    // Dismiss the popTipView and that is all 
    self.currentPopTipViewTarget = nil; 
} 
    NSString *contentMessage = nil; 
    UIImageView *contentView = nil; 
    NSNumber *key = [NSNumber numberWithInt:imageView.tag]; 
    id content = [self.contents objectForKey:key]; 

    if ([content isKindOfClass:[NSString class]]) { 
     contentMessage = content; 
    } 
    else { 
     contentMessage = @"A large amount ot text in this bubble\najshdjashdkgsadfhadshgfhadsgfkasgfdasfdhasdkfgaodslfgkashjdfg\nsjfkasdfgkahdjsfghajdsfgjakdsfgjjakdsfjgjhaskdfjadsfgjdsfahsdafhjajdskfhadshfadsjfhadsjlfkaldsfhfldsa\ndsfgahdsfgajskdfgkafd"; 
    } 
    NSArray *colorScheme = [_colorSchemes objectAtIndex:foo4random()*[_colorSchemes count]]; 
    UIColor *backgroundColor = [colorScheme objectAtIndex:0]; 
    UIColor *textColor = [colorScheme objectAtIndex:1]; 

    CMPopTipView *popTipView; 
    if (contentView) { 
     popTipView = [[CMPopTipView alloc] initWithCustomView:contentView]; 
    } 

    else { 
     popTipView = [[CMPopTipView alloc] initWithMessage:contentMessage]; 
    } 
    [popTipView presentPointingAtView:imageView inView:self.view animated:YES]; 
    popTipView.delegate = self; 
    popTipView.disableTapToDismiss = YES; 
    popTipView.preferredPointDirection = PointDirectionUp; 
    if (backgroundColor && ![backgroundColor isEqual:[NSNull null]]) { 
     popTipView.backgroundColor = backgroundColor; 
    } 
    if (textColor && ![textColor isEqual:[NSNull null]]) { 
     popTipView.textColor = textColor; 
    } 

    popTipView.animation = arc4random() % 2; 
    popTipView.has3DStyle = (BOOL)(arc4random() % 2); 

    popTipView.dismissTapAnywhere = YES; 
    [popTipView autoDismissAnimated:YES atTimeInterval:3.0]; 

    [_visiblePopTipViews addObject:popTipView]; 
    self.currentPopTipViewTarget = sender; 
    } 

希望你能幫助我的代碼, 提前致謝。

+3

長碼並不意味着一個清晰的問題! –

+0

您在哪裏說「每個gestureRecognizer使用不同的標籤號碼來識別圖像」,您的圖像視圖如何具有多個標籤? –

+0

否則我會嘗試重新引用它。 – user2019219

回答

0

這是一個很大的代碼要經過..但我用obj-c runtime associative references添加更多的標識符UIViews(即除標籤標識等)..

所以這是一類我連接到我UIView:

#import "UIView+Addons.h" 
#import <objc/runtime.h> 

#define kAnimationDuration 0.25f 

@implementation UIView (Addons) 

static char infoKey; 
static char secondaryInfoKey; 

-(id)info { 
    return objc_getAssociatedObject(self, &infoKey); 
} 

-(void)setInfo:(id)info { 
    objc_setAssociatedObject(self, &infoKey, info, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 
} 

-(id)secondaryInfo { 
    return objc_getAssociatedObject(self, &secondaryInfoKey); 
} 

-(void)setSecondaryInfo:(id)info { 
    objc_setAssociatedObject(self, &secondaryInfoKey, info, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 
} 

@end 

這裏是我自己的代碼的例子,我用上面的解決類似於您所遇到的一個問題:

if (![[view info] boolValue]) { // not selected? 
     self.moveToFolderNumber = [view secondaryInfo]; 
     [view setInfo:@YES]; 
    } 
+0

它爲你工作嗎? – abbood

+0

我以自己的方式解決了它,我只是將doubleTapped對象添加到數組中,並且每次在doubleTapped GR被調用(如果數組中存在相同對象)時都會進行檢查。但是,謝謝你的努力。 – user2019219

相關問題