【问题标题】:Identifying an UIImageView without tag识别没有标签的 UIImageView
【发布时间】:2013-08-08 23:49:48
【问题描述】:

我在滚动视图中有一些通用生成的 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;
     } 

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

【问题讨论】:

  • 长代码并不意味着一个明确的问题!
  • 你说“每个gestureRecognizer使用不同的标签号来识别图像”,你的图像视图怎么会有多个标签?
  • 我尝试改写它。
  • 我的 ImageViews 没有多个标签,但是我的 gestureRecognizer 根据 imageView 的标签号使用不同类型的识别方法,例如当我双击 ImageView 时,将调用 doubleTap 方法,但由于我说 imageView.tag = i; 它不再起作用当您现在查看 doubleTap 方法时,也许您会看到我的问题。
  • 将所有 imageViews 添加到一个数组中并使用indexOfObject:objectAtIndex:。如果您需要保留特定图像视图的列表,请将这些索引保存在 NSIndexSet 中。

标签: iphone ios objective-c macos ipad


【解决方案1】:

要处理很多代码.. 但是我通过使用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];
    }

【讨论】:

  • 我以自己的方式解决了这个问题,我只是将 doubleTapped 对象添加到数组中,并在每次调用 doubleTapped GR 时检查数组中是否存在相同的对象。但是,感谢您的努力。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-15
  • 1970-01-01
  • 2021-12-21
  • 1970-01-01
  • 2015-12-26
  • 2022-01-05
相关资源
最近更新 更多