识别没有标记的UIImageView

时间:2013-08-07 14:30:31

标签: iphone ios objective-c macos ipad

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

这里我动态生成ImageView:

-(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;
     } 

希望你能提前帮助我。

1 个答案:

答案 0 :(得分: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];
    }