| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- //
- // KSAudioAnimationView.m
- // KulexiuSchoolStudent
- //
- // Created by 王智 on 2023/8/29.
- //
- #import "KSAudioAnimationView.h"
- #define ANIMATION_RATIO (0.9)
- @interface KSAudioAnimationView ()
- // 💿旋转view
- @property (weak, nonatomic) IBOutlet UIView *discView;
- @property (weak, nonatomic) IBOutlet UIImageView *music_image;
- @property (nonatomic, strong) UIImageView *stylusImage;
- @end
- @implementation KSAudioAnimationView
- - (void)awakeFromNib {
- [super awakeFromNib];
- [self addSubview:self.stylusImage];
- self.stylusImage.frame = CGRectMake(132*ANIMATION_RATIO, 17*ANIMATION_RATIO, 43*ANIMATION_RATIO, 117*ANIMATION_RATIO);
- [self setAnchorPoint:CGPointMake(28.5 / 43*ANIMATION_RATIO, 25.0 / 117*ANIMATION_RATIO) forView:self.stylusImage];
- [self transformStylusView:YES];
- }
- - (void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)stylusImage {
- CGPoint oldOrigin = stylusImage.frame.origin;
- stylusImage.layer.anchorPoint = anchorPoint;
- CGPoint newOrigin = stylusImage.frame.origin;
- CGPoint transition;
- transition.x = newOrigin.x - oldOrigin.x;
- transition.y = newOrigin.y - oldOrigin.y;
- stylusImage.center = CGPointMake(stylusImage.center.x - transition.x, stylusImage.center.y - transition.y);
- }
- - (void)transformStylusView:(BOOL)isHide {
- if (isHide) {
- self.stylusImage.transform = CGAffineTransformIdentity;
- self.stylusImage.transform = CGAffineTransformMakeRotation(M_PI_2);
- }
- else {
- self.stylusImage.transform = CGAffineTransformIdentity;
- }
- }
- + (instancetype)shareInstance {
- KSAudioAnimationView *view = [[[NSBundle mainBundle] loadNibNamed:@"KSAudioAnimationView" owner:nil options:nil] firstObject];
- view.frame = CGRectMake(0, 0, 180, 180);
- return view;
- }
- - (void)setIsPlay:(BOOL)isPlay {
- _isPlay = isPlay;
- if (isPlay) {
- [self addRotationAnimation];
- [UIView animateWithDuration:1 animations:^{
- [self transformStylusView:NO];
- }];
- }
- else {
- [self removeAnimation];
- [UIView animateWithDuration:1 animations:^{
- [self transformStylusView:YES];
- }];
- }
- }
- - (void)addRotationAnimation {
- CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
- rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI *2.0];
- rotationAnimation.duration = 4.0f;
- rotationAnimation.cumulative = YES;
- rotationAnimation.repeatCount = CGFLOAT_MAX;
- [self.discView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
- }
- - (void)removeAnimation {
- [self.discView.layer removeAllAnimations];
- }
- - (void)configWithImageWithUrl:(NSString *)imageUrl {
- if ([NSString isEmptyString:imageUrl]) {
- [self.music_image setImage:[UIImage imageNamed:@"pub_music_placeholder"]];
- }
- else {
- [self.music_image sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
- }
- }
- - (UIImageView *)stylusImage {
- if (!_stylusImage) {
- _stylusImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"stylus_image"]];
- }
- return _stylusImage;
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
|