保持一个uiview突出显示(更改它的alpha),直到另一个点击

时间:2014-10-16 08:12:55

标签: ios objective-c uiview uiscrollview

如何突出显示UIVIew(更改alpha)直到另一个被点击?我有一个uiscrollview,我已经加载了几个uiview以实现缩略图效果。我使用这种方法创建它们:

        _thumbnailScroll.delegate = self;

        [_thumbnailScroll setBackgroundColor:[UIColor clearColor]];
        [_thumbnailScroll setCanCancelContentTouches:NO];

        _thumbnailScroll.indicatorStyle = UIScrollViewIndicatorStyleWhite;
        _thumbnailScroll.clipsToBounds = NO;
        _thumbnailScroll.scrollEnabled = YES;
        _thumbnailScroll.pagingEnabled = YES;

        NSUInteger nimages = 0;
        NSInteger tot=0;
        CGFloat cx = 0;
        for (nimages = 0; nimages < 5 ; nimages++) {
         //   NSString *imageName = [NSString stringWithFormat:@"thumb%lu.png", (nimages + 1)];
            UIImage *image = [UIImage imageWithContentsOfFile:[path stringByAppendingPathComponent:@"newPics/thumb/thumb1.png" ]];
            if (tot==5) {
                break;
            }
            if (5==nimages) {
                nimages=0;
            }

            UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

            CGRect rect = imageView.frame;
            rect.size.height = 150;
            rect.size.width = 150;
            rect.origin.x = cx;
            rect.origin.y = 0;

            imageView.frame = rect;

           // NSString *propertyName = [NSString stringWithFormat:@"image%lu", (unsigned long)nimages];
            //UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureCapturedFor1:)];
            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self    action:@selector(onSingleTapGestureRecognized:)];
            singleTap.numberOfTapsRequired = 1;
            singleTap.numberOfTouchesRequired = 1;
            singleTap.delegate = self;
//            [imageView add]
            [imageView addGestureRecognizer:singleTap];
            //[singleTap1 release];

            imageView.userInteractionEnabled = YES;

            [_thumbnailScroll addSubview:imageView];
            _thumbnailScroll.userInteractionEnabled = YES;
            // [imageView release];

            cx += imageView.frame.size.width+100;
            tot++;
        }

上述方法初始化滚动视图内的图像视图,并添加要在以下方法中捕获的点击手势:

- (void)onSingleTapGestureRecognized:(UITapGestureRecognizer *)singleTapGestureRecognizer
{

    UIView *tappedView = [_thumbnailScroll hitTest:[singleTapGestureRecognizer locationInView: _thumbnailScroll] withEvent:nil];
 // tappedView.alpha =1.0;

   // NSLog(@"image view tag %ld" , (long)tappedView.tag);

//    NSLog(@" Description %@ " ,   tappedView.description );
   // label.highlighted = YES;

    tappedView.alpha = 0.5;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 5.0 * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
     tappedView.alpha = 1.0;;
    });
 }

现在这个方法突出了点击的uiview几乎5秒,我试图保持突出显示,直到我点击另一个uiview。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

删除此代码:

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 5.0 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
 tappedView.alpha = 1.0;;
});

并替换为:

for(UIView *view in _thumbnailScroll.subviews) {
    view.alpha = 1.0;
}

这可以防止点击视图中的alpha在5秒后返回到1,并确保所有其他视图的alpha值为零。或者,对于更高效的方法,设置指向最后一个单击视图的指针,而不是循环遍历每个ScrollView的子视图,只需设置您引用的视图的alpha即1.0。因此,您需要添加名为lastTappedView的属性,然后将上述代码替换为:

_lastTappedView.alpha = 1.0;
_lastTappedView = tappedView;

答案 1 :(得分:0)

 singleTap.numberOfTouchesRequired = 1;

numberOfTouch是只读的。