在屏幕上显示UIImageView点击

时间:2014-03-27 17:05:19

标签: ios objective-c uiimageview

我想在用户点按UIImageView时显示图片。但在用户点击UIImageView之前,不应显示图像,几秒钟后图像应再次消失。有谁知道如何做到这一点?我阅读了几个线程,但它们不能与最新的Xcode一起使用。谢谢你的帮助和时间。

UDATE

好吧,我的代码现在看起来像这样:

    -(void)imageTapped:(UITapGestureRecognizer*)recognizer
    {
        recognizer.view.alpha=0.0;
        ((UIImageView*)recognizer.view).image = [UIImage imageNamed:@"twingo_main.png"];

        [UIView animateWithDuration:1.0 delay:2.0 options:0 animations:^{
            recognizer.view.alpha=1.0;
        } completion:^(BOOL finished) {
            recognizer.view.alpha=0.0;
            ((UIImageView*)recognizer.view).image = nil;
            recognizer.view.alpha=1.0;
        }];
    }

    - (void)viewDidLoad
    {

        UIImageView *hiddenImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 30, 20, 20)];
        hiddenImage.userInteractionEnabled=YES;
        [hiddenImage addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)]];
        [self.view addSubview:hiddenImage];

嗯,现在我的问题是,我如何在视图控制器中设置UIImageView?

5 个答案:

答案 0 :(得分:1)

UIViewController的viewDidLoad方法中:

    ...

UIImageView *hiddenImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 30, 20, 20)];
        hiddenImage.userInteractionEnabled=YES;
        [hiddenImage addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)]];
        [self.view addSubview:hiddenImage];
    ...

UITapGestureRecognizer处理程序:

-(void)imageTapped:(UITapGestureRecognizer*)recognizer
{
    recognizer.view.alpha=0.0;
    ((UIImageView*)recognizer.view).image = [UIImage imageNamed:@"imageName"];

    [UIView animateWithDuration:1.0 delay:2.0 options:0 animations:^{
        recognizer.view.alpha=1.0;
    } completion:^(BOOL finished) {
        recognizer.view.alpha=0.0;
        ((UIImageView*)recognizer.view).image = nil;
        recognizer.view.alpha=1.0;
    }];
}

答案 1 :(得分:0)

最初将UIImageView中的图像设置为nil。向UIImageView添加点击手势识别器,在触发时,设置图像并启动计时器。计时器完成后,将图像设置为零。

答案 2 :(得分:0)

以下是使用NSTimer处理上述内容的另一种方法..

    -(void)imageTapped:(UITapGestureRecognizer*)recognizer
    {    

        CustomPopUpView *lCustomPopUpView = [[CustomPopUpView alloc]init];
        //Added your imageview to Custom UIView class
        [self.window addSubview:lCustomPopUpView];
        [self.window bringSubviewToFront:lCustomPopUpView];
        mPopupTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self  selector:@selector(closePopUp) userInfo:Nil repeats:FALSE]; 

     }
    - (void)closePopUp{             

        if (mPopupTimer != nil) {
            [mPopupTimer invalidate];
            mPopupTimer = nil;
        }
        for (UIView *lView  in self.window.subviews) {
            if ([lView isKindOfClass:[CustomPopUpView class]]) {
                [lView removeFromSuperview];
            }
        }
    } 

答案 3 :(得分:0)

最好在UIImageView后面查看以处理TapGesture。 但是这里有你的代码修复:

将此添加到viewDidLoad方法以设置图像视图以处理点按手势

//By default the UserInteraction is disabled in UIImageView
       [self.testImageView setUserInteractionEnabled:YES];
        UITapGestureRecognizer *tapgesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(ShowImage:)];
        tapgesture.numberOfTapsRequired=1;
        tapgesture.numberOfTouchesRequired=1;

        [self.testImageView addGestureRecognizer:tapgesture];

这里是处理手势事件的方法

-(void)ShowImage:(UIGestureRecognizer*)recognizer{

    recognizer.view.alpha=0.0;
    ((UIImageView*)recognizer.view).image = [UIImage imageNamed:@"clone.jpg"];

    [UIView animateWithDuration:1.0 delay:0 options:0 animations:^{
        recognizer.view.alpha=1.0;
    } completion:^(BOOL finished) {
        //[self performSelector:@selector(hideImage:) withObject:recognizer.view afterDelay:10];
        [UIView animateWithDuration:1.0 delay:3.0 options:0 animations:^{
            recognizer.view.alpha=0.0;
        } completion:^(BOOL finished) {
            ((UIImageView*)recognizer.view).image=nil;
            ((UIImageView*)recognizer.view).alpha=1.0;
        }];
    }];

}

如果将视图的alpha设置为0,则视图将不会再接收任何触摸事件。因此,最佳做法是在从UIImageView删除图像后再次将其设置为1.0。

答案 4 :(得分:0)

您可能需要考虑使用UIButton。用这些来检测触摸很容易 - 就像改变它们的图像一样。

您也可以继承UIControl(参见http://www.raywenderlich.com/36288/how-to-make-a-custom-control)。