UIView アニメーションを使用してアルファを変更することで「パルス」する単純な UIButton サブクラスを作成しています。私が抱えている問題は、ユーザーがボタンをタッチしたときにアルファを 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];
}