如何在动画到alpha 0时触摸UIImageView?

时间:2012-10-27 07:39:35

标签: ios uigesturerecognizer alpha uiviewanimation touchesbegan

有人帮忙!

以下是我要实施的内容:

  • 当UIImageView变为alpha 0(隐藏)
  • 可以触及
  • 使其alpha成为一个(unhidden)。

但UIImageView在动画时不会被触摸(=变成alpha 0)。

我在stackoverflow上尝试了数百种技能,但没有用。

他们是.......

  1. UIViewAnimationOptionAllowUserInteraction
  2. setUserInteractionEnabled:YES;
  3. 的touchesBegan
  4. GestureRecognizer选项
  5. 等。
  6. 只有“hitTest”功能有效,但动画期间没有。

    请回复。谢谢。 以下是我的代码。

    #import "ViewController.h"
    #define AD @"text.png"
    #import <QuartzCore/QuartzCore.h>
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    
    @synthesize scrollData=_scrollData;
    @synthesize adImage=_adImage;
    
    
    
       - (void)viewDidLoad
        {
            //_adImage is UIImageView
            _adImage=[[adImage alloc] initWithImage:[UIImage imageNamed:AD]];
    _scrollData.scrollView.delegate = self;
    
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
    
            [_adImage addGestureRecognizer:singleTap];
            [_adImage setMultipleTouchEnabled:YES];
            [_adImage setUserInteractionEnabled:YES];
    
            [self.view addSubview:_adImage];
            _adImage.alpha=0;
            [_adImage setUp_pt:CGPointMake(160,250)];
            _adImage.center=_adImage.up_pt;
    
            [super viewDidLoad];
            [self hideImage:_adImage delay:0];
            [self becomeFirstResponder];
        }
    
    
        - (void)hideImageComplete:(UIView*)v
        {
            [self hideImage:v delay:0];
        }
    
        - (void)hideImage:(UIImageView*)v delay:(int)nDelay
        {
            [_adImage becomeFirstResponder];
    
            [UIView animateWithDuration:1
                                  delay:nDelay
                                options:
             (UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction)
                             animations: ^
             {
                     _adImage.alpha=0.0f;
             }
                             completion:^(BOOL completed){
                                 [self hideImageComplete:v];
                             }];
        }
    
    - (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
    {
        NSLog(@"Gesture event on view");
        _adImage.alpha=1;
    }
    
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    {
        return YES;
    }
    
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
        return YES;
    }
    
    
    
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        NSLog(@"hit!!");
        [super touchesBegan:touches withEvent:event];
        UITouch *touch = [touches anyObject];
        CGPoint touchPoint = [touch locationInView:self.view];
        CGPoint touchLocation = [touch locationInView:self.view];
        _adImage.alpha=1;
    
    }
    
    
    @end
    
    
    
    @implementation adImage
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        CGPoint touchPoint = [touch locationInView:self];
        if ([[[self layer] presentationLayer] hitTest:touchPoint]) {
            [self.layer removeAllAnimations];
        }
    }
    -(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
        if ([[self.layer presentationLayer] hitTest:point]) {
            NSLog(@"hit!!");
            self.alpha=1;
            return self;
        }
        return [super hitTest:point withEvent:event];
    }
    
    @synthesize up_pt;
    
    
    @end
    

    这是我的ViewController.h代码。

    #import <UIKit/UIKit.h>
    
    
    @interface adImage : UIImageView {
    }
    
    
    //@property (strong, nonatomic) UIImageView *imageView;
    
    @property (assign)CGPoint up_pt;
    
    
    @end
    
    
    
    @interface ViewController : UIViewController <UIScrollViewDelegate>{
    }
    
    - (void)hideImageComplete:(UIView*)v;
    - (void)hideImage:(UIView*)v delay:(int)nDelay;
    
    @property (strong, nonatomic) adImage *adImage;
    @property(nonatomic, strong) IBOutlet UIWebView *scrollData;
    
    
    @end
    

2 个答案:

答案 0 :(得分:2)

我有答案,但首先:

  • 总是用起始大写字母命名一个类,即AdImageView(它的图像视图也不是纯视图子类)

  • 我拿了你的代码,但注释了你可能需要或不需要的所有触摸方法

  • 这里的根本问题是如果alpha为0,UIGestureRecognizer将无法工作,因此你会看到alpha = 0的动画被分为两部分,几乎为0,然后为0.如果用户点击取消,然后最终完成块将视图返回到0

代码:

- (void)hideImage:(UIImageView*)v delay:(int)nDelay
{
    //[_adImage becomeFirstResponder];

    [UIView animateWithDuration:(3.0f - 0.1f)
                          delay:nDelay
                        options: (UIViewAnimationOptionCurveEaseIn | 
                                  UIViewAnimationOptionAllowUserInteraction)
                     animations: ^
     {
             _adImage.alpha=0.1f;
     }
                     completion:^(BOOL completed)
                     {
                        NSLog(@"completed=%d", completed);
                        if(completed) {
                            [UIView animateWithDuration:0.1f animations:^{
                                _adImage.alpha=0.0f;
                            }];
                        } else {
                            _adImage.alpha=1;
                        }
                     }];
}

- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
    NSLog(@"Gesture event on view");
    [_adImage.layer removeAllAnimations];
}

答案 1 :(得分:0)

这对我来说非常适合iOS7.1 +:

self.viewWithTapgesture.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.01];