UIButton 的layer 在加入动画之后,点击事件就失效了

rollrock1987 2014-08-02 10:30:35
以下是我的代码, 这里的btn在没有加入动画的时候,click是可以使用的,但是加入了 animation之后,btn在移动的时候,点击事件没有触发click函数。请问应该怎么样才能触发click。

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor grayColor];

UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchDown];
btn.layer.cornerRadius = 25;

btn.backgroundColor = [UIColor whiteColor];

[self.view addSubview:btn];


UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(240, 160) radius:100 startAngle:(0/180.0)*M_PI endAngle:((0-1)/(180.0))*M_PI clockwise:YES];

CAKeyframeAnimation *orbit = [CAKeyframeAnimation animationWithKeyPath:@"position"];
orbit.path = path.CGPath;
orbit.duration = 16;
orbit.repeatCount = HUGE_VALF;


[btn.layer addAnimation:orbit forKey:@"orbit"];


}

-(void)click
{
NSLog(@"click");
}
...全文
1095 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ReyZhang 2014-08-05
  • 打赏
  • 举报
回复
给CAKeyframeAnimation 再添加两个属性试试 CAKeyframeAnimation *orbit = [CAKeyframeAnimation animationWithKeyPath:@"position"]; orbit.path = path.CGPath; orbit.duration = 16; orbit.repeatCount = HUGE_VALF; /////new add orbit.removedOnCompletion = NO; orbit.fillMode= kCAFillModeForwards;
czl0325 2014-08-05
  • 打赏
  • 举报
回复
其实移动完位置又回到了原地,这个我早就碰到过了。
rollrock1987 2014-08-05
  • 打赏
  • 举报
回复
引用 9 楼 zhanglei5415 的回复:
给CAKeyframeAnimation 再添加两个属性试试 CAKeyframeAnimation *orbit = [CAKeyframeAnimation animationWithKeyPath:@"position"]; orbit.path = path.CGPath; orbit.duration = 16; orbit.repeatCount = HUGE_VALF; /////new add orbit.removedOnCompletion = NO; orbit.fillMode= kCAFillModeForwards;
是的 你的 new add里面的代码 我网上查到了 已经放到代码里面了的。
rollrock1987 2014-08-04
  • 打赏
  • 举报
回复
http://www.999dh.net/home.php?mod=space&uid=1&do=blog&id=410 写在这里的 做下记录
Bannings 2014-08-03
  • 打赏
  • 举报
回复
如果你想实现点击动画中的btn,得通过我上面给你代码,用hitTest+presentationLayer来实现
Bannings 2014-08-03
  • 打赏
  • 举报
回复
引用 2 楼 rollrock1987 的回复:
我找了相关的资料 说是 因为btn的 frame并没有移动 所以我现在想尝试 下 让这个 btn的 frame 围绕着 UIBezierPath 不知道这样应该怎么实现。
不管你怎么移动,CoreAnimation的一个核心就是虽然看起来btn的frame在持续改变(在动画中),但是其frame的设置是立即改变的,比如你把一个button从0,0移动到200,200,在这种情况下: 1.如果你使用的是显式动画(就像你现在用的CAKeyframeAnimation),是通过指定path来进行动画的,它的frame并没有改变,点击范围还是0,0这里 2.如果你使用的是隐式动画,是通过设置frame来进行动画的,那么它的点击范围就是200,200这里
rollrock1987 2014-08-03
  • 打赏
  • 举报
回复
我找了相关的资料 说是 因为btn的 frame并没有移动 所以我现在想尝试 下 让这个 btn的 frame 围绕着 UIBezierPath 不知道这样应该怎么实现。
rollrock1987 2014-08-03
  • 打赏
  • 举报
回复
我在self.view 上面家了一个 UIImageView,然后在UIImageView上面加了一个UIButton UIImageView添加了 tap事件,尝试了下 UIButton userinterfaceenable 一定要设置为NO ,否则不行。 至于直接加载self.view上面没有尝试。
Bannings 2014-08-03
  • 打赏
  • 举报
回复
引用 5 楼 rollrock1987 的回复:
[quote=引用 1 楼 zhangao0086 的回复:] CAKeyframeAnimation与CABasicAnimation下的对象都不能接收touch事件,如果你想做点击的话,只能自己在self.view(即添加btn的父视图)上面做了,具体的,给self.view添加一个UITapGestureRecognizer,然后在里面去hitTest:

-(void)click:(UITapGestureRecognizer *)gesture{
    CGPoint touchPoint = [gesture locationInView:self.view];
    if ([self.button.layer.presentationLayer hitTest:touchPoint]) {
        self.view.backgroundColor = self.view.backgroundColor == [UIColor yellowColor] ? [UIColor redColor] : [UIColor yellowColor];
    }
}
copy,还需要注意一个就是 btn的 userinterfaceenable 需要设置为false 这样才能使tap的触发让self.view 获取[/quote] userInteraction可以不用设置为false,这个主要看场景,为true的话可以照常接受btn的点击事件,只不过是在btn的frame范围内
Bannings 2014-08-03
  • 打赏
  • 举报
回复
CAKeyframeAnimation与CABasicAnimation下的对象都不能接收touch事件,如果你想做点击的话,只能自己在self.view(即添加btn的父视图)上面做了,具体的,给self.view添加一个UITapGestureRecognizer,然后在里面去hitTest:

-(void)click:(UITapGestureRecognizer *)gesture{
    CGPoint touchPoint = [gesture locationInView:self.view];
    if ([self.button.layer.presentationLayer hitTest:touchPoint]) {
        self.view.backgroundColor = self.view.backgroundColor == [UIColor yellowColor] ? [UIColor redColor] : [UIColor yellowColor];
    }
}
rollrock1987 2014-08-03
  • 打赏
  • 举报
回复
引用 1 楼 zhangao0086 的回复:
CAKeyframeAnimation与CABasicAnimation下的对象都不能接收touch事件,如果你想做点击的话,只能自己在self.view(即添加btn的父视图)上面做了,具体的,给self.view添加一个UITapGestureRecognizer,然后在里面去hitTest:

-(void)click:(UITapGestureRecognizer *)gesture{
    CGPoint touchPoint = [gesture locationInView:self.view];
    if ([self.button.layer.presentationLayer hitTest:touchPoint]) {
        self.view.backgroundColor = self.view.backgroundColor == [UIColor yellowColor] ? [UIColor redColor] : [UIColor yellowColor];
    }
}
copy,还需要注意一个就是 btn的 userinterfaceenable 需要设置为false 这样才能使tap的触发让self.view 获取

29,028

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧