Swift - 一个纯代码实现的登录界面带猫头鹰动画效果

这个是一个可爱的登录页面,动画效果仿自国外网站readme.io。在GitHub上有人写了个objective-C版本,这里我翻译成Swift版的分享给大家。

效果图如下:
(1)当输入用户名时,猫头鹰的手是搭在登录框上
(2)当输入密码时,猫头鹰的手会遮住眼睛。这个是有动画效果的。实现方式其实就是图片移动动画。
(3)离开密码框时,猫头鹰手又会放下。


动态效果如下:

代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import UIKit
class ViewController : UIViewController , UITextFieldDelegate {
//用户密码输入框
var txtUser: UITextField !
txtPwd: !
//左手离脑袋的距离
offsetLeftHand: CGFloat = 60
//左手图片,右手图片(遮眼睛的)
imgLeftHand: UIImageView !
imgRightHand: !
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:0px 1em!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:auto!important; background:none rgb(249,右手图片(圆形的)
imgLeftHandGone: UIImageView !
imgRightHandGone: !
//登录框状态
showType: LoginShowType = . NONE
override func viewDidLoad() {
super .viewDidLoad()
//获取屏幕尺寸
let mainSize = UIScreen .mainScreen().bounds.size
//猫头鹰头部
imgLogin = (frame: CGRectMake (mainSize.width/2-211/2,100,211,109))
imgLogin.image = UIImage (named: "owl-login" )
imgLogin.layer.masksToBounds = true
self .view.addSubview(imgLogin)
//猫头鹰左手(遮眼睛的)
rectLeftHand = (61 - offsetLeftHand,90,40,65)
imgLeftHand = (frame:rectLeftHand)
imgLeftHand.image = "owl-login-arm-left" )
imgLogin.addSubview(imgLeftHand)
//猫头鹰右手(遮眼睛的)
rectRightHand = (imgLogin.frame.size.width / 2 + 60,65)
imgRightHand = (frame:rectRightHand)
imgRightHand.image = UIImage "owl-login-arm-right" )
imgLogin.addSubview(imgRightHand)
//登录框背景
vLogin = UIView (15,200,mainSize.width - 30,160))
vLogin.layer.borderWidth = 0.5
vLogin.layer.borderColor = UIColor .lightGrayColor(). CGColor
vLogin.backgroundColor = .whiteColor()
.view.addSubview(vLogin)
//猫头鹰左手(圆形的)
rectLeftHandGone = CGRectMake (mainSize.width / 2 - 100,
vLogin.frame.origin.y - 22,40)
imgLeftHandGone = (frame:rectLeftHandGone)
imgLeftHandGone.image = "icon_hand" )
.view.addSubview(imgLeftHandGone)
//猫头鹰右手(圆形的)
rectRightHandGone = (mainSize.width / 2 + 62,
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:0px 1em!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:auto!important; background:none rgb(249,40)
imgRightHandGone = (frame:rectRightHandGone)
imgRightHandGone.image = )
.view.addSubview(imgRightHandGone)
//用户名输入框
txtUser = UITextField (30,30,vLogin.frame.size.width - 60,44))
txtUser.delegate = self
txtUser.layer.cornerRadius = 5
txtUser.layer.borderColor = CGColor
txtUser.layer.borderWidth = 0.5
txtUser.leftView = UIView (0,44,44))
txtUser.leftviewmode = UITextFieldviewmode Always
//用户名输入框左侧图标
imgUser = (frame: (11,11,22,22))
imgUser.image = "iconfont-user" )
txtUser.leftView!.addSubview(imgUser)
vLogin.addSubview(txtUser)
//密码输入框
txtPwd = ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,44))
txtPwd.delegate = self
txtPwd.layer.cornerRadius = 5
txtPwd.layer.borderColor = CGColor
txtPwd.layer.borderWidth = 0.5
txtPwd.secureTextEntry = true
txtPwd.leftView = ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,44))
txtPwd.leftviewmode = Always
//密码输入框左侧图标
imgPwd = ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,22))
imgPwd.image = "iconfont-password" )
txtPwd.leftView!.addSubview(imgPwd)
vLogin.addSubview(txtPwd)
}
//输入框获取焦点开始编辑
textFieldDidBeginEditing(textField: )
{
//如果当前是用户名输入
if textField.isEqual(txtUser){
(showType != PASS )
{
showType = USER
return
}
USER
//播放不遮眼动画
.animateWithDuration(0.5,animations: { () -> Void in
.imgLeftHand.frame = (
.imgLeftHand.frame.origin.x - .offsetLeftHand,
.imgLeftHand.frame.origin.y + 30,
.imgLeftHand.frame.size.width, .imgLeftHand.frame.size.height)
.imgRightHand.frame = (
.imgRightHand.frame.origin.x + 48,
.imgRightHand.frame.origin.y + 30,
.imgRightHand.frame.size.width,monospace!important; min-height:auto!important; background:none!important">.imgRightHand.frame.size.height)
.imgLeftHandGone.frame = (
.imgLeftHandGone.frame.origin.x - 70,
.imgLeftHandGone.frame.origin.y,40)
.imgRightHandGone.frame = (
.imgRightHandGone.frame.origin.x + 30,
.imgRightHandGone.frame.origin.y,40)
})
}
//如果当前是密码名输入
else textField.isEqual(txtPwd){
(showType == )
{
PASS
return
}
PASS
//播放遮眼动画
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,animations: { () -> Void in
(
.imgLeftHand.frame.origin.x + ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
.imgLeftHand.frame.origin.y - 30,
.imgLeftHand.frame.size.height)
.imgRightHand.frame = (
.imgRightHand.frame.origin.x - 48,
.imgRightHand.frame.origin.y - 30,
.imgRightHand.frame.size.height)
.imgLeftHandGone.frame = (
.imgLeftHandGone.frame.origin.x + 70,
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:0px 1em!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:auto!important; background:none rgb(249,0)
(
.imgRightHandGone.frame.origin.x - 30,
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:0px 1em!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:auto!important; background:none rgb(249,0)
})
}
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
}
//登录框状态枚举
enum {
case NONE
USER
PASS
}
源码下载: hangge_926.zip
原文出自: www.hangge.com 转载请保留原文链接 http://www.hangge.com/blog/cache/detail_927.html

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...