Swift 走马灯

我们常见走马灯样式的功能,下面整理一下 Object-C 与 Swift 的实现代码

OC
UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(10,200, self.view.bounds.size.width, 100)];
label3.backgroundColor = [UIColor redColor];
label3.text =@"走马灯 走马灯 走马灯 走马灯 走马灯 走马灯 走马灯 走马灯~~~";
[self.view addSubview:label3];

CGRect frame = label3.frame;
frame.origin.x = -180;
label3.frame = frame;
[UIView beginAnimations:@"testAnimation"context:NULL];
[UIView setAnimationDuration:8.8f];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDelegate:self];
[UIView setAnimationRepeatAutoreverses:NO];
[UIView setAnimationRepeatCount:999999];
frame = label3.frame;
frame.origin.x =350;
label3.frame = frame;
[UIView commitAnimations];
Swift 
//添加上计算文字长度,优化动画效果
let str : NSString = "走马灯 走马灯 走马灯 走马灯 走马灯 走马灯 走马灯 走马灯......"
let font : UIFont = UIFont.systemFont(ofSize: 14)
let attrs : NSDictionary = [NSFontAttributeName : font]
let size : CGSize = str.boundingRect(with: CGSize.init(width: kScreenWidth, height: CGFloat(MAXFLOAT)), options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: attrs as! [String : Any], context: nil).size
let lab = UIFactory.create_ALabel(text: str as String, superView: self.view)
lab.frame = CGRect(x: 15, y: 10, width: kScreenWidth, height: 20)
lab.textColor = UIColor.hrgb("666666")
lab.font = font
var frame : CGRect = lab.frame
frame.origin.x = size.width
lab.frame = frame
UIView.beginAnimations("testAnimation", context: nil)
UIView.setAnimationDuration(8.8)
UIView.setAnimationCurve(.linear)
UIView.setAnimationDelegate(self)
UIView.setAnimationRepeatAutoreverses(false)
UIView.setAnimationRepeatCount(999999)
frame = lab.frame
frame.origin.x = -size.width
lab.frame = frame
UIView.commitAnimations()
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,612评论 4 61
  • 第001章 穿越 装饰现代、豪华、清新的办公室中一名身着白色连衣裙的美貌少女正慵懒的坐在棕色的真皮转椅上,眼睛轻...
    倾听尘世喧嚣阅读 3,973评论 2 2
  • 你不会知道一个人有多爱你 因为他热切挂念你的时候恰好你都不在 我看着清晨的露珠在窗外由花苞上滴落 蜗牛在花香中缓步...
    活着至少疯一回阅读 3,101评论 0 2
  • 每个人人生表现方式不一样,在这个社会上存在的意义和价值也不一样,追求的也不一样,但是他们都有一个相似点,就是坚持做...
    玫瑰西海岸阅读 2,635评论 0 2

友情链接更多精彩内容