Ios 当UIImageView设置为alpha 0动画时,如何触摸它?
谁来帮忙 以下是我想要实现的:Ios 当UIImageView设置为alpha 0动画时,如何触摸它?,ios,uigesturerecognizer,alpha,uiviewanimation,touchesbegan,Ios,Uigesturerecognizer,Alpha,Uiviewanimation,Touchesbegan,谁来帮忙 以下是我想要实现的: 当UIImageView变为alpha 0(隐藏)时 它可以触摸 所以它的alpha变成了1(未隐藏) 但UIImageView在设置动画(=变成alpha 0)时不会被触摸 我在stackoverflow尝试了上百种技能,但都没有成功 他们是 UIViewAnimationOptionAllowUserInteraction setUserInteractionEnabled:是 接触开始了 手势识别器选项 等等 只有功能“hitTest”起作用,但在动画期
- 当UIImageView变为alpha 0(隐藏)时
- 它可以触摸
- 所以它的alpha变成了1(未隐藏)
#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”
#定义AD@“text.png”
#进口
@界面视图控制器()
@结束
@实现视图控制器
@综合scrollData=\u scrollData;
@合成一幅图像=_一幅图像;
-(无效)viewDidLoad
{
//_A图像是UIImageView
_adImage=[[adImage alloc]initWithImage:[UIImage IMAGENAME:AD]];
_scrollData.scrollView.delegate=self;
UITapGestureRecognizer*singleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTapGestureCaptured:)];
[\u adImage AddgestureRecognitor:singleTap];
[\u adImage SETMULTIPLETOUCHED:是];
[\u adImage setUserInteractionEnabled:是];
[self.view addSubview:_adImage];
_a图像α=0;
[_a图像设置\u pt:CGPointMake(160250)];
_adImage.center=\u adImage.up\u pt;
[超级视图下载];
[自隐藏图像:图像延迟:0];
[自我成为第一反应者];
}
-(void)hideImageComplete:(UIView*)v
{
[自隐藏图像:v延迟:0];
}
-(无效)隐藏图像:(UIImageView*)v延迟:(int)显示
{
[_adImage成为第一反应者];
[UIView animateWithDuration:1
延迟:nDelay
选项:
(UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionLowUserInteraction)
动画:^
{
_A图像α=0.0f;
}
完成:^(BOOL已完成){
[自隐藏图像完成:v];
}];
}
-(无效)singleTapGestureCaptured:(UITapGestureRecognitizer*)手势
{
NSLog(“查看手势事件”);
_a图像α=1;
}
-(BOOL)手势识别器:(UIGestureRecognizer*)手势识别器应与gestureRecognizer:(UIGestureRecognizer*)其他手势识别器同时识别
{
返回YES;
}
-(BOOL)手势识别器:(UIGestureRecognitor*)手势识别器应接受触摸:(UITouch*)触摸{
返回YES;
}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
NSLog(@“命中!!”);
[超级触摸开始:触摸事件:事件];
UITouch*touch=[触摸任何对象];
CGPoint touchPoint=[触摸位置视图:self.view];
CGPoint-touchLocation=[touch-locationInView:self.view];
_a图像α=1;
}
@结束
@实现一个映像
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
UITouch*touch=[触摸任何对象];
CGPoint接触点=[touch locationInView:self];
if([[self layer]presentationLayer]命中测试:接触点]){
[self.layer removeAllAnimations];
}
}
-(UIView*)hitTest:(CGPoint)point with event:(UIEvent*)event{
if([[self.layer presentationLayer]命中测试:点]){
NSLog(@“命中!!”);
自身α=1;
回归自我;
}
返回[super hitTest:pointwithevent:event];
}
@合成uppt;
@结束
这是我的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
#导入
@接口adImage:UIImageView{
}
//@属性(强,非原子)UIImageView*imageView;
@财产(分配)CGPoint up\U pt;
@结束
@界面ViewController:UIViewController{
}
-(void)hideImageComplete:(UIView*)v;
-(void)hideImage:(UIView*)v delay:(int)nDelay;
@属性(强,非原子)adImage*adImage;
@属性(非原子,强)IBUIWebView*scrollData;
@结束
我有答案给你,但首先:
- 总是用大写字母命名一个类,例如AdImageView(它是一个图像视图,不是纯视图子类)
- 我拿走了你的代码,但注释掉了你所有的触摸方法,你可能需要也可能不需要
- 这里的根本问题是,如果alpha为0,UIGestureRecognitor将不起作用,因此您将看到alpha=0的动画分成两部分,几乎是0,然后是0。如果用户点击取消,则最终完成块将视图返回到0
这在iOS7.1+上对我非常有效:
self.viewWithTapgesture.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.01];
self.viewWithTapgesture.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.01];