为什么我的setAlpha动画停止按钮接收发件人?

时间:2013-01-10 09:00:14

标签: ios animation uibutton alpha

我想要一个闪烁按钮,然后我这样编码 我按下这样的按钮:

_btn = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
[_btn addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];
[_btn setBackgroundImage:[UIImage imageNamed:@"img2.jpg"] forState:UIControlStateNormal];

我将这样的动画编码:

[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse animations:^{
    _btn.alpha = 0.2;
} completion:nil];

按钮含糊不清但无法选择,无法接收发件人。 任何人都知道为什么它无法接收发件人?

2 个答案:

答案 0 :(得分:1)

试试这个

[UIView animateWithDuration:1 delay:0 options: UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse animations:^{
    _btn.alpha = 0.2;
} completion:nil];

我在上面的代码中插入了这个选项

UIViewAnimationOptionAllowUserInteraction

答案 1 :(得分:1)

我没有t know why it can收到发件人,可能是因为阿尔法一直在变化。但我有办法解决它。你可以创建一个后台按钮,它可以接收发件人,你可以像这样编码:

    _btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    _btn.userInteractionEnabled = NO;
    [_btn setBackgroundImage:[UIImage imageNamed:@"img2.jpg"] forState:UIControlStateNormal];


    _btn1 = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
    [_btn1 addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];
    [_btn1 setBackgroundColor:[UIColor clearColor]];
    [_btn1 addSubView : _btn];

然后像代码一样设置动画

[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse animations:^{
    _btn.alpha = 0.2;
} completion:nil];

button1将收到发件人