Swift 版本很好的卡片切换效果基于ZLSwipeableView
前言:在这篇文章你可以学到,一些基本的Swift语法, 基本UI控件闭包等.
实际的效果,比gif图的效果好很多.
首先需要导入ZLSwipeableView
pod 'ZLSwipeableView', '~> 0.0.8'
下面是代码
// 1. 签协议
class ViewController: UIViewController, ZLSwipeableViewDelegate, ZLSwipeableViewDataSource {
// 2, 随便定义一个标题数组
let titles = [
"Teame",
"Sea",
"Em",
"Nes",
"Pver",
"Beole",
"Amst",
"Wisria",
"Whalt",
"Midue",
"Suwer",
"Orage",
]
var index = 0
var xtSwipeableView: ZLSwipeableView?
在viewDidLoad
xtSwipeableView = ZLSwipeableView.init()
xtSwipeableView!.frame = CGRectMake(55, 110, self.view.frame.size.width - 110, self.view.frame.size.height - 220)
xtSwipeableView!.delegate = self
xtSwipeableView!.dataSource = self
xtSwipeableView!.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(xtSwipeableView!)
self.setButton()
创建 ⬆️,⬇️,⬅️,右btn
func setButton()
{
var items = ["上", "下", "左", "右"]
for i in 0...3{
let btn = UIButton.init(type: UIButtonType.Custom)
self.view.addSubview(btn)
btn.frame = CGRectMake(50 + 60 * (CGFloat)(i), self.view.frame.size.height - 90, 50, 50)
btn.setTitle(items[i], forState: UIControlState.Normal)
btn.addTarget(self, action: "handle:", forControlEvents: UIControlEvents.TouchUpInside)
btn.tag = i
}
}
btn点击方法
func handle(btn: UIButton)
{
let tagType = btn.tag
switch tagType{
case 0:
xtSwipeableView!.swipeTopViewToUp()
case 1:
xtSwipeableView!.swipeTopViewToDown()
case 2:
xtSwipeableView!.swipeTopViewToLeft()
case 3:
xtSwipeableView!.swipeTopViewToRight()
default:
print("....")
}
}
loadViews
override func viewDidLayoutSubviews() {
xtSwipeableView!.loadViewsIfNeeded()
}
ZLSwipeableViewDataSource
func nextViewForSwipeableView(swipeableView: ZLSwipeableView!) -> UIView! {
if self.index >= self.titles.count {
self.index = 0
}
let view = CardView.init(frame: swipeableView.bounds)
view.backgroundColor = UIColor.purpleColor()
view.textLabel?.text = self.titles[index]
// 闭包回调
view .initWithClosure(addressThatTakesAClosure)
self.index++
return view
}
func addressThatTakesAClosure(string:String) ->Void{
// do you something
print("\(string)")
}
CardView类的实现(在这你可以定义你所需要的)
class CardView: UIView {
typealias sendValueClosure = (string:String)->Void
/// 声明一个Closure(闭包)
var myClosure:sendValueClosure?
/// 下面这个方法需要传入上个界面的someFunctionThatTakesAClosure函数指针
func initWithClosure(closure:sendValueClosure?){
myClosure = closure
}
///
var imagesView: UIImageView?
var textLabel : UILabel?
var btn: UIButton?
///
override init(frame: CGRect) {
super.init(frame: frame)
self.setView(frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/// setView
func setView(frame: CGRect){
// Shadow
self.layer.shadowColor = UIColor.lightGrayColor().CGColor
self.layer.shadowOpacity = 0.33
self.layer.shadowOffset = CGSizeMake(0, 1.5)
self.layer.shadowRadius = 4.0
self.layer.shouldRasterize = true
self.layer.rasterizationScale = UIScreen.mainScreen().scale
// Corner Radius
self.layer.cornerRadius = 10.0
// Custom view
imagesView = UIImageView.init(frame: CGRectMake(5, 5, self.frame.size.width - 10, self.frame.size.height / 2))
imagesView!.backgroundColor = UIColor.whiteColor()
self.addSubview(imagesView!)
textLabel = UILabel.init(frame: CGRectMake(20, imagesView!.frame.size.height + 10, 120, 20))
textLabel!.backgroundColor = UIColor.lightGrayColor()
self.addSubview(textLabel!)
btn = UIButton.init(type: UIButtonType.Custom)
btn?.setTitle("BUTTON", forState: UIControlState.Normal)
btn?.frame = CGRectMake(20, (textLabel?.frame.origin.y)! + 20 + 10, 100, 50)
self.addSubview(btn!)
btn?.addTarget(self, action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
}
/// 这里实现了btn点击方法的回调, 回到控制器(VC),可实现跳转
func btnClick()
{
if myClosure != nil{
self.myClosure!(string: "hello World")
}
}
}
总结: 希望看到这篇文章对读者有一定Swift语言的帮助, 且完成卡片切换效果需求. 喜欢请点赞. 稍候会把项目传到github.今天有点晚了.睡了 呼呼. -.-
demo参考(Objective-C & Swift) :
https://github.com/Zhangjingwang1993/XSmomoStyle
觉得还可以请点个赞, 我要申请微博加V. 点个赞吧客官 -.-
个人技术博客站欢迎您
原文链接:http://www.jianshu.com/p/734962c9bbed
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
Swift 版本很好的卡片切换效果基于ZLSwipeableView的更多相关文章
- Swift 版本号非常好的卡片切换效果基于ZLSwipeableView(相似于[陌陌点点][探探])
这是我在简书的文章. http://www.jianshu.com/p/734962c9bbed
- jQuery手机端触摸卡片切换效果
效果:http://hovertree.com/code/run/jquery/a1gr3gm9.html 可以用手机查看效果. 代码如下: <!doctype html> <htm ...
- tab切换的效果——仿照今日头条APP的切换效果
说点废话;不知道是哪一版本起头条的tab切换效果就变了,一直琢磨着这个事,去度娘那里也没有什么结果:正好这两天有空就尝试做了一下:用前端的技术来实现: 先看效果吧:上面的tab随着slide滑动,上面 ...
- Swift - UITableView状态切换效果
Swift - UITableView状态切换效果 效果 源码 https://github.com/YouXianMing/Swift-Animations // // TableViewTapAn ...
- 很实用的HTML5+CSS3注册登录窗体切换效果
1. [代码]3个很实用的HTML5+CSS3注册登录窗体切换效果 <!DOCTYPE html><!--[if lt IE 7 ]> <html lang=" ...
- [Swift通天遁地]九、拔剑吧-(7)创建旋转和弹性的页面切换效果
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [Swift通天遁地]九、拔剑吧-(8)创建气泡式页面切换效果
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [Swift通天遁地]九、拔剑吧-(15)搭建具有滑出、视差、3D变形等切换效果的引导页
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 窥探Swift系列博客说明及其Swift版本间更新
Swift到目前为止仍在更新,每次更新都会推陈出新,一些Swift旧版本中的东西在新Swift中并不适用,而且新版本的Swift会添加新的功能.到目前为止,Swift为2.1版本.去年翻译的Swift ...
随机推荐
- 2013-07-24 IT 要闻速记快想
### ========================= ###凡客有闹钟?从凡客的角度来讲,闹钟等工具类应用是为推广品牌和产品服务,通过工具类产品给大众一个对凡客品牌的认知.而选择推出工具类的产品 ...
- WPF中实现Button.Content变化的简易动画
项目中曾要这样的需求——输入法的切换,要求从English切换到简体中文的时候,Button的Content先从English变成空白,再从空白变成简体中文, 而不是直接从English变成简体中文. ...
- python学习第二天第二部分
一.变量:用来记录状态 变量值得变化即状态的变化,程序运行的本质就是来处理一系列状态的变化 python中所有数据都是对象 对象的三个特性: 身份(内存地址):用id()获取 类型:决定了该对象可以保 ...
- Xaml代码格式设置
Xaml格式化后属性分行显示的设置方法为 找到Tools->Text Editor->Xaml->Formatting->Spacing,选择Position each att ...
- ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
测试库一条update语句报错:ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction mysql> ...
- glog使用
How To Use Google Logging Library Glog 的基本使用方法在google code上有介绍:How To Use Google Logging Library ;最好 ...
- ActiveMQ使用记录
1.在Linux中安装ActiveMQ 官方文档地址:http://activemq.apache.org/getting-started.html#GettingStarted-StartingAc ...
- MVC初学 - The type or namespace name 'DbContext' could not be found
问题: The type or namespace name 'DbContext' could not be found (are you missing a using directive or ...
- Lua基础之字符串(string)
1,计算字符串长度 2,返回字符串s的n个拷贝 3,返回字符串全部字母大写 4,返回字符串全部字母小写 5,返回一个类似printf的格式化字符串 6,根据下标截取字符串 7,在字符串中查找 8,在字 ...
- How to Implement Bluetooth Low Energy (BLE) in Ice Cream Sandwich
ShareThis - By Vikas Verma Bluetooth low energy (BLE) is a feature of Bluetooth 4.0 wireless radio t ...