1

我在滚动视图中有一些通用生成的 ImageView,这些图像中的每一个都有 2 个手势识别器,用于在 ImageView 上单击/双击。现在我的问题是如何识别 ImageView 是第一次还是第二次被点击。在某些情况下,使用 ImageView 的标签很容易,但我在每个 ImageView 上有两个不同的手势识别器,每个手势识别器使用基于标签号的不同识别方法来识别图像。

在这里,我动态生成 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;
     } 

希望你能帮助我,在此先感谢。

4

1 回答 1

0

这是很多代码要经过..但是我通过使用obj-c 运行时关联引用向 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];
    }
于 2013-08-07T15:28:08.470 回答