IOS控件系列---文本上下滚动的标签实现广告
来源:互联网 发布:秋冬用水乳 知乎 编辑:程序博客网 时间:2024/06/06 03:15
一.效果:
实现思路较简单,这里不作分析了,直接上代码:
import Foundation
import UIKit
/// 滚动标签点击事件
protocol ScrollLabelListener {
func onClickListener(view :UIView)
}
/// 上下滚动的标签视图--常用于上下滚动的广告标签中
class ScrollLabelView: UIView {
var delegate :ScrollLabelListener?
var titles :Array<String>?
var nTitleIdx :Int = 1
var nIdx :Int = 1
var labelSize :CGRect = CGRect.zero
/*
Swift 中不加修饰的 init方法都需要在方法中保证所有非 Optional 的实例变量被赋值初始化
*/
overrideinit(frame: CGRect) {
super.init(frame: frame)
}
requiredinit?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func initWithTitles(titles :Array<String>, andFrame:CGRect) {
self.titles = titles
self.labelSize = andFrame
let btn :UIButton = UIButton.init(frame :CGRect.init(x:0, y: 0, width: andFrame.width, height: andFrame.height))
btn.tag =self.nIdx
btn.setTitleColor(UIColor.black, for: .normal)
btn.setTitle(self.titles?[0], for: .normal)
btn.addTarget(self, action:#selector(onClick), for: .touchUpInside)
self.addSubview(btn)
Timer.scheduledTimer(timeInterval:3,
target:self,
selector:#selector(self.buildNextLabel),
userInfo:nil,
repeats:true)
}
func buildNextLabel() {
self.nTitleIdx =self.nTitleIdx % (self.titles?.count)!
self.nIdx =self.nIdx % (self.titles?.count)! == 0 ? (self.titles?.count)! :self.nIdx % (self.titles?.count)!
let oldBtn :UIButton = self.viewWithTag(self.nIdx)as! UIButton
let newBtn :UIButton = UIButton.init(frame:CGRect.init(x:0, y: labelSize.height, width:labelSize.width, height:labelSize.height))
newBtn.tag =self.nIdx +1 > (self.titles?.count)! ?1 : self.nIdx +1
newBtn.addTarget(self, action:#selector(onClick), for: .touchUpInside)
newBtn.setTitleColor(UIColor.black, for: .normal)
newBtn.setTitle(self.titles?[self.nTitleIdx], for: .normal)
self.addSubview(newBtn)
UIView.animate(withDuration:0.25, animations: {() -> Voidin
oldBtn.setY(y: -self.labelSize.height)
newBtn.setY(y:0)
}, completion: {(finished) ->Void in
oldBtn.removeFromSuperview()
})
self.nIdx +=1
self.nTitleIdx +=1
}
func bindClickEvent(clickEvent :ScrollLabelListener) {
delegate = clickEvent
}
func onClick(button :UIButton) {
iflet tmpClick = delegate{
tmpClick.onClickListener(view: button)
}
}
}
- IOS控件系列---文本上下滚动的标签实现广告
- 实现TextView文本的上下滚动
- 通过TextSwitcher实现上下滚动的广告动画效果
- iOS文字上下滚动效果的实现
- iOS走马灯上下滚动显示文本
- 随鼠标上下滚动的广告条
- Android 上下滚动的广告条
- JS实现文本内容上下滚动效果
- 单行上下滚动广告
- 实现上下滚动的TextView
- 实现上下滚动的TextView
- 实现上下滚动的TextView
- javascript上下滚动文本
- Android_RecyclerView实现上下滚动广告条(带图片)
- 自定义控件实现文本滚动
- 随滚动条上下滑动的广告效果
- 一个不错的jquery上下滚动广告展示教程
- Android TextSwitche 上下滚动的广告条 小喇叭功能
- Storm环境配置及吞吐量测试调优总结
- vue 常用Api整理 4
- vue打开新页面
- Android 多语言翻译文件导入和检查
- ul,ol,dl的区别
- IOS控件系列---文本上下滚动的标签实现广告
- Jquery中的$(this)和JS中的this的差别
- 数据结构与C语言实现(七)——树(下):集合与集合例题
- 图表编辑器TMS Diagram Studio更新至v4.14,支持RAD Studio 10.2 Tokyo
- 判断素数
- 教你如何在微信浏览器唤醒APP
- Java从入门到精通索引
- C/C++ 开发库 | C/C++ Development Library
- D3D11 立方体贴图(天空盒子)