iOS使用 CABasicAnimation 實(shí)現(xiàn)簡(jiǎn)單的跑馬燈(無(wú)cpu暴漲)
網(wǎng)上找了幾個(gè),但都有cup暴漲的情況發(fā)生,于是利用CABasicAnimation 簡(jiǎn)單的實(shí)現(xiàn)一個(gè)跑馬燈,實(shí)現(xiàn)簡(jiǎn)單,可自己定制
#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"];
// 動(dòng)畫(huà)選項(xiàng)的設(shè)定
animation.duration = 2; // 持續(xù)時(shí)間
animation.repeatCount = MAXFLOAT; // 重復(fù)次數(shù)
animation.removedOnCompletion = NO;
animation.autoreverses = NO;
// 起始幀和終了幀的設(shè)定
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)]; // 終了幀
// 添加動(dòng)畫(huà)
[AD.layer addAnimation:animation forKey:@"AnimationMoveY"];
// [headImage.layer addAnimation:animation forKey:@"AnimationMoveY"];
}
-(void)headimage{
animationhead = [CABasicAnimation animationWithKeyPath:@"position"];
// 動(dòng)畫(huà)選項(xiàng)的設(shè)定
animationhead.duration = 2; // 持續(xù)時(shí)間
animationhead.repeatCount = MAXFLOAT; // 重復(fù)次數(shù)
animationhead.removedOnCompletion = NO;
animationhead.autoreverses = NO;
// 起始幀和終了幀的設(shè)定
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)]; // 終了幀
// 添加動(dòng)畫(huà)
[headImage.layer addAnimation:animationhead forKey:@"AnimationMoveY"];
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS無(wú)障礙適配西瓜視頻Voice?Over實(shí)踐示例
本文從研發(fā)的視角出發(fā),講述了如何使用?Voice?Over、如何適配?Voice?Over?以及適配過(guò)程中如果遇到問(wèn)題應(yīng)該如何解決。希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
iOS實(shí)現(xiàn)scrollview上拉顯示Navbar下拉隱藏功能詳解
這篇文章主要給大家介紹了利用iOS實(shí)現(xiàn)scrollview上拉顯示Navbar下拉隱藏功能的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-05-05
iOS開(kāi)發(fā)之隨機(jī)生成兩圓之間的標(biāo)準(zhǔn)圓
這篇文章主要給大家介紹了iOS如何實(shí)現(xiàn)在兩圓之間隨機(jī)生成標(biāo)準(zhǔn)圓的方法,實(shí)現(xiàn)的效果類(lèi)似尋找附近人或者附近商家的動(dòng)態(tài)效果,有需要的朋友可以參考借鑒,下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-01-01
詳解iOS App開(kāi)發(fā)中session和coockie的用戶(hù)數(shù)據(jù)存儲(chǔ)處理
iOS在HTTP網(wǎng)絡(luò)編程環(huán)境方面提供了NSURLSession、NSHTTPCookieStorage和NSHTTPCookie類(lèi)來(lái)處理session和coockie的相關(guān)內(nèi)容,接下來(lái)我們將來(lái)詳解iOS App開(kāi)發(fā)中session和coockie的用戶(hù)數(shù)據(jù)存儲(chǔ)處理:2016-06-06
iOS使用UICountingLabel實(shí)現(xiàn)數(shù)字變化的動(dòng)畫(huà)效果
本文主要介紹了iOS使用UICountingLabel實(shí)現(xiàn)數(shù)字變化動(dòng)畫(huà)效果的方法,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2016-12-12
iOS 統(tǒng)計(jì)Xcode項(xiàng)目代碼行數(shù)的實(shí)例
下面小編就為大家分享一篇iOS 統(tǒng)計(jì)Xcode項(xiàng)目代碼行數(shù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
iOS開(kāi)發(fā)中使用UILabel設(shè)置字體的相關(guān)技巧小結(jié)
這篇文章主要介紹了iOS開(kāi)發(fā)中UILabel設(shè)置字體的相關(guān)技巧小結(jié),代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01
iOS runtime forwardInvocation詳解及整理
這篇文章主要介紹了 iOS runtime forwardInvocation詳解及整理的相關(guān)資料,需要的朋友可以參考下2017-02-02
iOS實(shí)現(xiàn)自定義起始時(shí)間選擇器視圖
本篇文章主要介紹了iOS實(shí)現(xiàn)自定義起始時(shí)間選擇器視圖,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06

