欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS使用 CABasicAnimation 实现简单的跑马灯(无cpu暴涨)

程序员文章站 2024-02-16 22:08:58
网上找了几个,但都有cup暴涨的情况发生,于是利用cabasicanimation 简单的实现一个跑马灯,实现简单,可自己定制 #import

网上找了几个,但都有cup暴涨的情况发生,于是利用cabasicanimation 简单的实现一个跑马灯,实现简单,可自己定制

#import <uikit/uikit.h>

@interface loopview : uiview
@property(nonatomic,strong)nsstring *iformation;
@property(nonatomic,strong)nsstring *image;
@end
#import "loopview.h"
#import "uiimageview+webcache.h"
@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.frame.size.width, self.frame.size.height)];
    [self addsubview:ad];

    ad.textcolor = tjxcolor(146, 152, 169);
    ad.font = [uifont fontwithname:@"gotham-book" size:12*wpscreen_width_ratio];
    ad.textalignment = nstextalignmentleft;
    headimage = [[uiimageview alloc]initwithframe:cgrectmake(0, 0, 20*wpscreen_width_ratio, 20*wpscreen_width_ratio)];

    [self addsubview:headimage];

    headimage.layer.cornerradius = 10;
    headimage.layer.maskstobounds = yes;

    [self sssss];
    [self headimage];
  }
  return self;
}
-(void)setiformation:(nsstring *)iformation{
    ad.text =iformation;
}
-(void)setimage:(nsstring *)image{

   [headimage sd_setimagewithurl:[nsurl urlwithstring:image]];
}
-(void)sssss{


  animation = [cabasicanimation animationwithkeypath:@"position"];
  // 动画选项的设定
  animation.duration = 2; // 持续时间
  animation.repeatcount = maxfloat; // 重复次数
  animation.removedoncompletion = no;
  animation.autoreverses = no;
  // 起始帧和终了帧的设定
  animation.fromvalue = [nsvalue valuewithcgpoint:cgpointmake(ad.layer.position.x, 30*wpscreen_higth_ratio)]; // 起始帧
  animation.tovalue = [nsvalue valuewithcgpoint:cgpointmake(ad.layer.position.x, ad.layer.position.y-20*wpscreen_higth_ratio)]; // 终了帧
  // 添加动画
  [ad.layer addanimation:animation forkey:@"animationmovey"];
//  [headimage.layer addanimation:animation forkey:@"animationmovey"];

}
-(void)headimage{
  animationhead = [cabasicanimation animationwithkeypath:@"position"];
  // 动画选项的设定
  animationhead.duration = 2; // 持续时间
  animationhead.repeatcount = maxfloat; // 重复次数
  animationhead.removedoncompletion = no;
  animationhead.autoreverses = no;
  // 起始帧和终了帧的设定
  animationhead.fromvalue = [nsvalue valuewithcgpoint:cgpointmake(headimage.layer.position.x, 30*wpscreen_higth_ratio)]; // 起始帧
  animationhead.tovalue = [nsvalue valuewithcgpoint:cgpointmake(headimage.layer.position.x, headimage.layer.position.y-20*wpscreen_higth_ratio)]; // 终了帧
  // 添加动画
  [headimage.layer addanimation:animationhead forkey:@"animationmovey"];

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。