Skip to content

Commit 35d3c56

Browse files
committed
Refined animation code to be less redundant
1 parent 16d05b4 commit 35d3c56

File tree

1 file changed

+18
-46
lines changed

1 file changed

+18
-46
lines changed

TORoundedButton/TORoundedButton.m

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,10 @@ - (UIView *)_makeBackgroundViewWithStyle:(TORoundedButtonBackgroundStyle)style T
209209
backgroundView.frame = self.bounds;
210210
backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
211211

212+
backgroundView.clipsToBounds = !TORoundedButtonIsSolidBackground(style);
212213
if (@available(iOS 26.0, *)) {
213214
backgroundView.cornerConfiguration = _cornerConfiguration;
214215
} else {
215-
backgroundView.clipsToBounds = !TORoundedButtonIsSolidBackground(style);
216216
backgroundView.layer.cornerRadius = _cornerRadius;
217217
}
218218

@@ -390,99 +390,71 @@ - (void)_didDragInside {
390390

391391
#pragma mark - Animation -
392392

393+
- (void)_performTapAnimation:(void (^)(void))animations
394+
completion:(void (^_Nullable)(BOOL finished))completion TOROUNDEDBUTTON_OBJC_DIRECT {
395+
[UIView animateWithDuration:_tapAnimationDuration
396+
delay:0.0f
397+
usingSpringWithDamping:1.0f
398+
initialSpringVelocity:0.5f
399+
options:UIViewAnimationOptionBeginFromCurrentState
400+
animations:animations
401+
completion:completion];
402+
}
403+
393404
- (void)_setBackgroundColorTappedAnimated:(BOOL)animated TOROUNDEDBUTTON_OBJC_DIRECT {
394405
if (!_tappedTintColor || !TORoundedButtonIsTintableBackground(_backgroundStyle)) { return; }
395406

396-
// Toggle the background color of the title label
397-
void (^updateTitleOpacity)(void) = ^{
398-
self->_titleLabel.backgroundColor = [self _labelBackgroundColor];
399-
};
400-
401-
// -----------------------------------------------------
402-
403407
UIColor *const destinationColor = _isTapped ? _tappedTintColor : self.tintColor;
404408
void (^animationBlock)(void) = ^{
405409
[self _setBackgroundTintColor:destinationColor];
406410
};
407411

408412
void (^completionBlock)(BOOL) = ^(BOOL completed){
409413
if (completed == NO) { return; }
410-
updateTitleOpacity();
414+
self->_titleLabel.backgroundColor = [self _labelBackgroundColor];
411415
};
412416

413417
if (!animated) {
414418
animationBlock();
415419
completionBlock(YES);
416-
}
417-
else {
420+
} else {
418421
_titleLabel.backgroundColor = [UIColor clearColor];
419-
[UIView animateWithDuration:_tapAnimationDuration
420-
delay:0.0f
421-
usingSpringWithDamping:1.0f
422-
initialSpringVelocity:0.5f
423-
options:UIViewAnimationOptionBeginFromCurrentState
424-
animations:animationBlock
425-
completion:completionBlock];
422+
[self _performTapAnimation:animationBlock completion:completionBlock];
426423
}
427424
}
428425

429426
- (void)_setLabelAlphaTappedAnimated:(BOOL)animated TOROUNDEDBUTTON_OBJC_DIRECT {
430427
if (_tappedTextAlpha > 1.0f - FLT_EPSILON) { return; }
431428

432429
const CGFloat alpha = _isTapped ? _tappedTextAlpha : 1.0f;
433-
434-
// Animate the alpha value of the label
435430
void (^animationBlock)(void) = ^{
436431
self->_titleLabel.alpha = alpha;
437432
};
438433

439-
// If we're not animating, just call the blocks manually
440434
if (!animated) {
441-
// Remove any animations in progress
442435
[_titleLabel.layer removeAnimationForKey:@"opacity"];
443436
animationBlock();
444437
return;
445438
}
446439

447-
// Set the title label to clear beforehand
448440
_titleLabel.backgroundColor = [UIColor clearColor];
449-
450-
// Animate the button alpha
451-
[UIView animateWithDuration:_tapAnimationDuration
452-
delay:0.0f
453-
usingSpringWithDamping:1.0f
454-
initialSpringVelocity:0.5f
455-
options:UIViewAnimationOptionBeginFromCurrentState
456-
animations:animationBlock
457-
completion:nil];
441+
[self _performTapAnimation:animationBlock completion:nil];
458442
}
459443

460444
- (void)_setButtonScaledTappedAnimated:(BOOL)animated TOROUNDEDBUTTON_OBJC_DIRECT {
461445
if (_tappedButtonScale < FLT_EPSILON) { return; }
462446

463447
const CGFloat scale = _isTapped ? _tappedButtonScale : 1.0f;
464-
465-
// Animate the scale value of the label
466448
void (^animationBlock)(void) = ^{
467-
self->_containerView.transform = CGAffineTransformScale(CGAffineTransformIdentity,
468-
scale,
469-
scale);
449+
self->_containerView.transform = CGAffineTransformScale(CGAffineTransformIdentity, scale, scale);
470450
};
471451

472-
// If we're not animating, just call the blocks manually
473452
if (!animated) {
474453
animationBlock();
475454
return;
476455
}
477456

478-
// Animate the button alpha
479-
[UIView animateWithDuration:_tapAnimationDuration
480-
delay:0.0f
481-
usingSpringWithDamping:1.0f
482-
initialSpringVelocity:0.5f
483-
options:UIViewAnimationOptionBeginFromCurrentState
484-
animations:animationBlock
485-
completion:nil];
457+
[self _performTapAnimation:animationBlock completion:nil];
486458
}
487459

488460
#pragma mark - Public Accessors -

0 commit comments

Comments
 (0)