最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS 實現(xiàn)跑馬燈效果的方法示例

 更新時間:2017年01月15日 09:34:01   作者:irembeu  
可能說起跑馬燈,大家第一個會想到的就是山寨機。但接下來這篇文章介紹的跑馬燈和那個跑馬燈是不一樣滴。在iOS中,跑馬燈是指label上的字自動滾動,形成類似跑馬燈似的條幅。下面通過這篇文章我們來一起看看iOS 實現(xiàn)跑馬燈效果的方法,有需要的朋友們可以參考借鑒。

在網(wǎng)頁開發(fā)當中跑馬燈是常用到的,用來顯示通知等,在游戲開發(fā)當中也如此。

首先來看看效果圖:

接下來就簡單看看這效果是怎么實現(xiàn)的。

實現(xiàn)方法

1、首先我們從這個圖片里面能聯(lián)想到如果實現(xiàn)這個效果必然需要使用到動畫,或者還有有用scrollView的思路,這里我是用的動畫的方式實現(xiàn)的。

2、.h文件

自定義一個繼承UIView的LGJAutoRunLabel類,在.h文件中:

@class LGJAutoRunLabel;

typedef NS_ENUM(NSInteger, RunDirectionType) {
 LeftType = 0,
 RightType = 1,
};

@protocol LGJAutoRunLabelDelegate <NSObject>

@optional
- (void)operateLabel: (LGJAutoRunLabel *)autoLabel animationDidStopFinished: (BOOL)finished;

@end

@interface LGJAutoRunLabel : UIView

@property (nonatomic, weak) id <LGJAutoRunLabelDelegate> delegate;
@property (nonatomic, assign) CGFloat speed;
@property (nonatomic, assign) RunDirectionType directionType;

- (void)addContentView: (UIView *)view;
- (void)startAnimation;
- (void)stopAnimation;

定義一個NS_ENUM用來判斷自動滾動的方向,分別是左和右,聲明一個可選類型的協(xié)議,用來在controller中調(diào)用并對autoLabel進行操作。聲明對外的屬性和方法。這里一目了然,主要的實現(xiàn)思路都集中在.m文件中。

3、.m文件

聲明“私有”變量和屬性:

@interface LGJAutoRunLabel()<CAAnimationDelegate>
{
 CGFloat _width;
 CGFloat _height;
 CGFloat _animationViewWidth;
 CGFloat _animationViewHeight;
 BOOL _stoped;
 UIView *_contentView;//滾動內(nèi)容視圖
}
@property (nonatomic, strong) UIView *animationView;//放置滾動內(nèi)容視圖
@end

初始化方法:

- (instancetype)initWithFrame:(CGRect)frame {

 if (self == [super initWithFrame:frame]) {
 _width = frame.size.width;
 _height = frame.size.height;

 self.speed = 1.0f;
 self.directionType = LeftType;
 self.layer.masksToBounds = YES;
 self.animationView = [[UIView alloc] initWithFrame:CGRectMake(_width, 0, _width, _height)];
 [self addSubview:self.animationView];
 }
 return self;
}

將滾動內(nèi)容視圖contentView添加到動畫視圖animationView上:

- (void)addContentView:(UIView *)view {

 [_contentView removeFromSuperview];
 view.frame = view.bounds;
 _contentView = view;
 self.animationView.frame = view.bounds;
 [self.animationView addSubview:_contentView];

 _animationViewWidth = self.animationView.frame.size.width;
 _animationViewHeight = self.animationView.frame.size.height;
}

讓animationView上的contentView自動滾動起來的主要方法在這兒,重點來了,就是這個- (void)startAnimation方法,看一下這個方法里面是怎么樣實現(xiàn)的:

- (void)startAnimation {
 [self.animationView.layer removeAnimationForKey:@"animationViewPosition"];
 _stoped = NO;

 CGPoint pointRightCenter = CGPointMake(_width + _animationViewWidth / 2.f, _animationViewHeight / 2.f);
 CGPoint pointLeftCenter = CGPointMake(-_animationViewWidth / 2, _animationViewHeight / 2.f);
 CGPoint fromPoint = self.directionType == LeftType ? pointRightCenter : pointLeftCenter;
 CGPoint toPoint  = self.directionType == LeftType ? pointLeftCenter : pointRightCenter;

 self.animationView.center = fromPoint;
 UIBezierPath *movePath = [UIBezierPath bezierPath];
 [movePath moveToPoint:fromPoint];
 [movePath addLineToPoint:toPoint];

 CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
 moveAnimation.path   = movePath.CGPath;
 moveAnimation.removedOnCompletion = YES;
 moveAnimation.duration  = _animationViewWidth / 30.f * (1 / self.speed);
 moveAnimation.delegate  = self;
 [self.animationView.layer addAnimation:moveAnimation forKey:@"animationViewPosition"];
}

↘️首先先把animationView.layer上的動畫移除掉,接下來就是要找到animationView\contentView的pointCenter這里把這個中點當做是animationView或者是contentView都行,因為這兩個視圖的frame是相等的,這步找左右中點的意義在于,完全顯示出文字內(nèi)容,因為可能會遇到那種比如文字太長了,view長度不夠,不能完全顯示出來文字的全部內(nèi)容, 這里我們找到中點,也就相當于確定了內(nèi)容視圖要滑動的范圍了,接下來根據(jù)起始方向的枚舉值設(shè)置fromPoint和toPoint這里我們默認是從右向左滾動的。這里我們做動畫設(shè)置,用到了貝塞爾曲線,我們設(shè)置UIBezierPath的起始位置就是fromPoint也就是屏幕右方(我們看不見)self.animationView.center。終止位置是屏幕左方toPoint此時animationView滾動的起始位置的首和終止位置的尾的距離正好是屏幕的寬度。這里我們使用CAKeyframeAnimation關(guān)鍵幀動畫,moveAnimation.path                 = movePath.CGPath; ,moveAnimation.removedOnCompletion  = YES;動畫完成后就將動畫移除,不保留最終的狀態(tài)。 [self.animationView.layer addAnimation:moveAnimation forKey:@"animationViewPosition"];將動畫添加到animationView.layer上。

(這段是對上面代碼的解釋)

-------------------分割線-------------------

接下來的這個就是代理方法的實現(xiàn)了,當動畫完成后悔調(diào)用LGJAutoRunLabelDelegate我們自定義的delegate方法。

- (void)stopAnimation {
 _stoped = YES;
 [self.animationView.layer removeAnimationForKey:@"animationViewPosition"];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
 if (self.delegate && [self.delegate respondsToSelector:@selector(operateLabel:animationDidStopFinished:)]) {
 [self.delegate operateLabel:self animationDidStopFinished:flag];
 }
 if (flag && !_stoped) {
 [self startAnimation];
 }
}

4、在controller中使用方法

主要的方法就是聲明LGJAutoRunLabel實例,將代理設(shè)為自身,聲明directionType默認為Left,在runLabel上創(chuàng)建label也就是我們看到的文字。其余方法一目了然了。

//MARK:- CreateAutoRunLabel
- (void)createAutoRunLabel {
 LGJAutoRunLabel *runLabel = [[LGJAutoRunLabel alloc] initWithFrame:CGRectMake(0, 100, kWidth, 50)];
 runLabel.delegate = self;
 runLabel.directionType = LeftType;
 [self.view addSubview:runLabel];
 [runLabel addContentView:[self createLabelWithText:@"繁華聲 遁入空門 折煞了夢偏冷 輾轉(zhuǎn)一生 情債又幾 如你默認 生死枯等 枯等一圈 又一圈的 浮圖塔 斷了幾層 斷了誰的痛直奔 一盞殘燈 傾塌的山門 容我再等 歷史轉(zhuǎn)身 等酒香醇 等你彈 一曲古箏" textColor:[selfrandomColor] labelFont:[UIFont systemFontOfSize:14]]];
 [runLabel startAnimation];
}

- (UILabel *)createLabelWithText: (NSString *)text textColor:(UIColor *)textColor labelFont:(UIFont *)font {
 NSString *string = [NSString stringWithFormat:@"%@", text];
 CGFloat width = [UILabel getWidthByTitle:string font:font];
 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 50)];
 label.font = font;
 label.text = string;
 label.textColor = textColor;
 return label;
}

- (UIColor *)randomColor {
 return [UIColor colorWithRed:[self randomValue] green:[self randomValue] blue:[self randomValue] alpha:1];
}

- (CGFloat)randomValue {
 return arc4random()%255 / 255.0f;
}

總結(jié)

這個例子挺小的,主要思路就是利用動畫將其變活,可能不太好理解的地方就是在動畫移動的path這個路徑的距離上,我們想這個路徑的時候肯定要這樣想,我讓動畫從最初我看不見的地方出現(xiàn),然后最后到我看不見的地方消失,這個中間的距離之差就是屏幕的寬度了,而這個屏幕的寬度正好我們可以用contentView.frame來表示。

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

相關(guān)文章

  • 淺談iOS 屏幕方向那點事兒

    淺談iOS 屏幕方向那點事兒

    這篇文章主要介紹了淺談iOS 屏幕方向那點事兒,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • IOS百度地圖導(dǎo)航開發(fā)功能實現(xiàn)簡述

    IOS百度地圖導(dǎo)航開發(fā)功能實現(xiàn)簡述

    百度地圖導(dǎo)航非常實用,那么基于代碼是如何實現(xiàn)的呢,下面通過本文給大家介紹IOS百度地圖導(dǎo)航開發(fā)功能實現(xiàn)簡述,需要的朋友可以參考下本文
    2016-03-03
  • iOS如何開發(fā)簡單的手繪應(yīng)用實例詳解

    iOS如何開發(fā)簡單的手繪應(yīng)用實例詳解

    這篇文章主要給大家介紹了關(guān)于iOS如何開發(fā)簡單的手繪應(yīng)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • 詳談iPhoneX截圖如何帶

    詳談iPhoneX截圖如何帶

    下面小編就為大家分享一篇詳談iPhoneX截圖如何帶"劉海"和圓角,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • 實例講解iOS中的CATransition轉(zhuǎn)場動畫使用

    實例講解iOS中的CATransition轉(zhuǎn)場動畫使用

    CATransition類為應(yīng)用程序的轉(zhuǎn)場動畫提供了很多可控制參數(shù),接下來我們就以幾個實例講解iOS中的CATransition轉(zhuǎn)場動畫使用,需要的朋友可以參考下
    2016-06-06
  • iOS啟動頁倒計時跳過按鈕功能

    iOS啟動頁倒計時跳過按鈕功能

    這篇文章主要介紹了iOS啟動頁倒計時跳過按鈕功能,需要的朋友可以參考下
    2017-07-07
  • IOS繪制虛線的方法總結(jié)

    IOS繪制虛線的方法總結(jié)

    這篇文章給大家分享了iOS中繪制虛線常見的幾種方式,大家可以根據(jù)自己的需求進行選擇哪種方法,下面跟著小編來一起看看吧。
    2016-09-09
  • Objective-C中利用正則去除非數(shù)字字母漢字方法實例

    Objective-C中利用正則去除非數(shù)字字母漢字方法實例

    正則表達式對我們?nèi)粘i_發(fā)來說是必不可少的,下面這篇文章主要給大家介紹了關(guān)于Objective-C中如何利用正則去除非數(shù)字字母漢字的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧
    2018-06-06
  • iOS實現(xiàn)簡易的導(dǎo)航欄顏色漸變實例代碼

    iOS實現(xiàn)簡易的導(dǎo)航欄顏色漸變實例代碼

    很多APP 都有導(dǎo)航欄顏色漸變的效果,下面這篇文章主要給大家介紹了關(guān)于iOS如何實現(xiàn)簡易的導(dǎo)航欄顏色漸變效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧
    2018-10-10
  • iOS UIPickerView的簡單封裝示例

    iOS UIPickerView的簡單封裝示例

    這篇文章主要給大家介紹了關(guān)于iOS UIPickerView的簡單封裝的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對各位iOS開發(fā)者們具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-06-06

最新評論

普宁市| 惠州市| 博白县| 正蓝旗| 黄大仙区| 余干县| 徐闻县| 丰县| 礼泉县| 莒南县| 民权县| 温宿县| 依兰县| 津市市| 宁都县| 东乌| 琼海市| 靖西县| 鸡东县| 郯城县| 洛南县| 化隆| 怀远县| 宣化县| 乐平市| 南漳县| 永安市| 黄大仙区| 甘肃省| 社旗县| 聂荣县| 广灵县| 余姚市| 裕民县| 忻州市| 大英县| 阿瓦提县| 武威市| 子洲县| 绿春县| 麦盖提县|