我正在创建一个简单的UIButton子类,通过使用UIView动画更改alpha来“脉冲”。我遇到的问题是当用户触摸按钮时将alpha设置为1(100%)。我有代码暂停和恢复动画,但在调用pauseAnimations之前或之后设置不透明度似乎不起作用。
在UIView的图层上暂停/恢复动画:
-(void)pauseAnimation {
CFTimeInterval pausedTime = [self convertTime:CACurrentMediaTime()
fromLayer:nil];
self.speed = 0.0;
self.timeOffset = pausedTime;
}
-(void)resumeAnimation {
CFTimeInterval pausedTime = [self timeOffset];
self.speed = 1.0;
self.timeOffset = 0.0;
self.beginTime = 0.0;
CFTimeInterval timeSincePause = [self convertTime:CACurrentMediaTime()
fromLayer:nil] - pausedTime;
self.beginTime = timeSincePause;
}
UIButton子类的代码:
-(id)initWithAnimationInterval:(NSTimeInterval)interval andMinimumAlpha:
(float)minimumAlpha andMaximumAlpha:(float)maximumAlpha {
if ((self = [super init])) {
_animationInterval = interval;
_minimumAlpha = minimumAlpha;
_maximumAlpha = maximumAlpha;
self.alpha = self.minimumAlpha;
[self addTarget:self action:@selector(buttonDown)
forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:@selector(buttonUp)
forControlEvents:UIControlEventTouchUpInside];
[self addTarget:self action:@selector(buttonUp)
forControlEvents:UIControlEventTouchUpOutside];
[self startAnimating];
}
return self;
}
-(void)startAnimating {
[UIView animateWithDuration:self.animationInterval
delay:0
options:UIViewAnimationOptionAllowAnimatedContent |
UIViewAnimationOptionAllowUserInteraction |
UIViewAnimationOptionAutoreverse |
UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat
animations:^{
self.alpha = self.maximumAlpha;
}
completion:^(BOOL finished) {
}];
}
-(void)buttonDown {
[self.layer pauseAnimation];
self.oldAlpha = self.alpha;
self.alpha = self.maximumAlpha;
}
-(void)buttonUp {
self.alpha = self.oldAlpha;
[self.layer resumeAnimation];
}
答案 0 :(得分:0)
尝试从动画选项中删除UIViewAnimationOptionRepeat。如果你在那里使用它意味着动画将无限重复。因此,停止计时器没有用,因为动画会继续重复。
您是否考虑过使用NSTimer?这似乎更简单。这是我做的:
- (void)viewDidLoad
{
[super viewDidLoad];
[button addTarget:self action:@selector(buttonDown) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(buttonUp) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(buttonUp) forControlEvents:UIControlEventTouchUpOutside];
[self startTimer];
}
- (void)startTimer {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.4 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
}
- (void)stopTimer {
[timer invalidate];
}
- (void)buttonDown {
[self stopTimer];
button.alpha = 1;
}
- (void)buttonUp {
[self startTimer];
}
- (void)timerFired:(NSTimer *)timer {
[UIView animateWithDuration:0.4
delay:0
options:UIViewAnimationOptionAllowAnimatedContent |
UIViewAnimationOptionAllowUserInteraction |
UIViewAnimationOptionAutoreverse |
UIViewAnimationOptionCurveEaseInOut
animations:^{
button.alpha = MIN_ALPHA;
}
completion:^(BOOL finished) {
button.alpha = 1;
}];
}
我没有使用按钮的子类,但您可以轻松地将其移动到那里。
希望有所帮助。
答案 1 :(得分:0)
除非您使用自定义核心动画图层,否则您可能希望暂停和恢复方法看起来像这样(来自http://developer.apple.com/library/ios/#qa/qa1673/_index.html):
-(void)pauseLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
layer.speed = 0.0;
layer.timeOffset = pausedTime;
}
-(void)resumeLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer timeOffset];
layer.speed = 1.0;
layer.timeOffset = 0.0;
layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
layer.beginTime = timeSincePause;
}
并传递button.layer
作为CALayer。如果您不想传递图层,请将self.speed
和self.timeOffset
等引用更改为self.layer.speed
和self.layer.timeOffset
,因为这些属性仅适用于图层,而不是按钮本身。