zoukankan      html  css  js  c++  java
  • OC 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( 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,  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()
     
  • 相关阅读:
    swift
    swift
    c# 根据自定义Attribute排序
    asp.net 导出excel
    算法时间复杂度的计算 [整理]
    再谈javascript原型继承
    深入学习JavaScript: apply 方法 详解(转)——非常好
    Chapter 6 : Control Statements : Looping
    Chapter 6 : Applications of Definite Integrals
    朋友圈仅三天可见?怎么破?
  • 原文地址:https://www.cnblogs.com/HMJ-29/p/7085615.html
Copyright © 2011-2022 走看看