【问题标题】:How can an UIImageView can be touched while it is animating to alpha 0?UIImageView 在动画到 alpha 0 时如何被触摸?
【发布时间】:2012-10-27 07:39:35
【问题描述】:

有人帮忙!

这是我想要实现的:

  • 当 UIImageView 变为 alpha 0(隐藏)时
  • 可以触摸
  • 使其 alpha 变为 1(未隐藏)。

但是 UIImageView 在动画时没有被触摸(=变成 alpha 0)。

我在stackoverflow上尝试了一百个技能,但没有奏效。

他们是.......

  1. UIViewAnimationOptionAllowUserInteraction
  2. setUserInteractionEnabled:YES;
  3. 触摸开始
  4. 手势识别器选项
  5. 等等..

只有函数“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

【问题讨论】:

    标签: ios uigesturerecognizer alpha uiviewanimation touchesbegan


    【解决方案1】:

    我有答案,但首先:

    • 总是以大写字母命名一个类,即 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];
    }
    

    【讨论】:

      【解决方案2】:

      这对我在 iOS7.1+ 上非常有用:

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

      【讨论】:

        猜你喜欢
        • 2011-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多