SoFunction
Updated on 2025-04-14

iOS uses CABasicAnimation to achieve simple marquee (no CPU surge)

I found a few online, but all the cups surged. So I used CABasicAnimation to simply implement a marquee. The implementation is simple and can be customized by yourself.

#import <UIKit/>

@interface LoopView : UIView
@property(nonatomic,strong)NSString *Iformation;
@property(nonatomic,strong)NSString *Image;
@end
#import ""
#import "UIImageView+"
@interface LoopView(){
  UILabel *AD;
  UIImageView *headImage;
  CABasicAnimation *animation;
   CABasicAnimation *animationhead;
}
@end
@implementation LoopView
-(instancetype)initWithFrame:(CGRect)frame{

  self = [super initWithFrame:frame];
  if (self) {
    AD = [[UILabel alloc]initWithFrame:CGRectMake(28*WPSCREEN_WIDTH_RATIO, 0, , )];
    [self addSubview:AD];

     = TJXColor(146, 152, 169);
     = [UIFont fontWithName:@"Gotham-Book" size:12*WPSCREEN_WIDTH_RATIO];
     = NSTextAlignmentLeft;
    headImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20*WPSCREEN_WIDTH_RATIO, 20*WPSCREEN_WIDTH_RATIO)];

    [self addSubview:headImage];

     = 10;
     = YES;

    [self sssss];
    [self headimage];
  }
  return self;
}
-(void)setIformation:(NSString *)Iformation{
     =Iformation;
}
-(void)setImage:(NSString *)Image{

   [headImage sd_setImageWithURL:[NSURL URLWithString:Image]];
}
-(void)sssss{


  animation = [CABasicAnimation animationWithKeyPath:@"position"];
  // Setting of animation options   = 2; // Duration   = MAXFLOAT; // Number of repetitions   = NO;
   = NO;
  // Settings of the start frame and the end frame   = [NSValue valueWithCGPoint:CGPointMake(, 30*WPSCREEN_HIGTH_RATIO)]; // Start frame   = [NSValue valueWithCGPoint:CGPointMake(, -20*WPSCREEN_HIGTH_RATIO)]; // End frame  // Add animation  [ addAnimation:animation forKey:@"AnimationMoveY"];
//  [ addAnimation:animation forKey:@"AnimationMoveY"];

}
-(void)headimage{
  animationhead = [CABasicAnimation animationWithKeyPath:@"position"];
  // Setting of animation options   = 2; // Duration   = MAXFLOAT; // Number of repetitions   = NO;
   = NO;
  // Settings of the start frame and the end frame   = [NSValue valueWithCGPoint:CGPointMake(, 30*WPSCREEN_HIGTH_RATIO)]; // Start frame   = [NSValue valueWithCGPoint:CGPointMake(, -20*WPSCREEN_HIGTH_RATIO)]; // End frame  // Add animation  [ addAnimation:animationhead forKey:@"AnimationMoveY"];

}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.