Iphone 设置按钮的动画

Iphone 设置按钮的动画,iphone,objective-c,ios4,uibutton,Iphone,Objective C,Ios4,Uibutton,我想知道是否有可能有一个UIButton,它在未被触动的情况下会产生动画 我不确定这是否有道理。我有一个UIButton,我通过IB将它放在我的XIB上。我正在使用一个自定义类型和我自己的75x75映像作为控件状态默认值。当用户触摸按钮时,我没有自定义图像设置,所以它会变暗,这很好 但是,当按钮未被触摸时,我希望它在两个或三个不同的自定义图像之间设置动画,使其在外边缘周围产生“发光”的效果。这可能吗?您需要一个自定义视图,因为UIButton无法制作动画。 包含两个子视图的整个框架的UIView

我想知道是否有可能有一个UIButton,它在未被触动的情况下会产生动画

我不确定这是否有道理。我有一个UIButton,我通过IB将它放在我的XIB上。我正在使用一个自定义类型和我自己的75x75映像作为控件状态默认值。当用户触摸按钮时,我没有自定义图像设置,所以它会变暗,这很好


但是,当按钮未被触摸时,我希望它在两个或三个不同的自定义图像之间设置动画,使其在外边缘周围产生“发光”的效果。这可能吗?

您需要一个自定义视图,因为UIButton无法制作动画。 包含两个子视图的整个框架的UIView: 按钮位于底部,uiimageview位于顶部

将uiimageview的animationImages属性设置为帧。
根据需要隐藏/显示uiimageview。

您可能需要转到核心动画层。UIButton层的阴影属性是可设置动画的,因此您可以创建一个重复的CABasicAnimation,我可以更改阴影颜色/大小并将其添加到层中。下面的代码适用于我

button.layer.shadowPath = [[UIBezierPath bezierPathWithRect:CGRectInset(button.bounds, -3, -3)] CGPath];
button.layer.shadowColor = [[UIColor whiteColor] CGColor];
button.layer.shadowOpacity = 1.0f;

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"shadowColor"];
animation.fromValue = (id)[[UIColor darkGrayColor] CGColor];
animation.toValue = (id)[[UIColor lightGrayColor] CGColor];
animation.duration = 1.0f;
animation.autoreverses = YES;
animation.repeatCount = NSIntegerMax;

[button.layer addAnimation:animation forKey:@"shadow"];
并将这些方法附加到按钮的控制事件

-(IBAction)userDidTouchDown:(UIButton *)sender
{
    [button.layer removeAnimationForKey:@"shadow"];
}

-(IBAction)userDidTouchUp:(UIButton *)sender
{

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"shadowColor"];
    animation.fromValue = (id)[[UIColor darkGrayColor] CGColor];
    animation.toValue = (id)[[UIColor lightGrayColor] CGColor];
    animation.duration = 1.0f;
    animation.autoreverses = YES;
    animation.repeatCount = NSIntegerMax;

    [button.layer addAnimation:animation forKey:@"shadow"];
}
您可以使用阴影值(特别是路径)使其适合您。别忘了附加userDidTouchUp:具有内部修补和外部修补事件