从今天开始,我学习的重点开始转向Swift,并且会分享一些自己学习的心得体会,今天给大家带来的的是无限轮播。广告页的无限轮播是非常常见的一个功能,大多数APP都有,大多数程序员也都实现过,今天我们用Swift实现一下。项目地址
图片切换我们可以选择的基本控件有两个UIScrollView 和 UICollectionView,这次我们选择UICollectionView;既然是轮播,就会用到Timer。所以,我们这次主要应用的知识点为UICollectionView 和 Timer;
代码如下 | 复制代码 |
import UIKit
classCycleScrollView: UIView, UICollectionViewDelegate,UICollectionViewDataSource {
var bottomView : UICollectionView? var width : CGFloat? var height : CGFloat? var timer : Timer?
override init(frame: CGRect){
super.init(frame: frame) // 1.设置背景色 self.backgroundColor = UIColor.clear // 2.设置宽高 width = self.frame.size.width height = self.frame.size.height // 3.添加bottomView setupBottomView() // 4.添加定时器 setupTimer() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }
func setupBottomView() {
// 5.设置collectionView的布局 let flowLayout = UICollectionViewFlowLayout(); flowLayout.itemSize = self.bounds.size flowLayout.minimumLineSpacing = 0; flowLayout.minimumInteritemSpacing = 0; flowLayout.scrollDirection = UICollectionViewScrollDirection.horizontal; bottomView = UICollectionView.init(frame: self.bounds, collectionViewLayout: flowLayout) self.addSubview(bottomView!); // 6.设置collectionView的尺寸 bottomView?.contentSize = CGSize(width:width! * CGFloat(4),height:height!) // 7.分页 bottomView?.isPagingEnabled =true // 8.去掉滚动条 bottomView?.showsVerticalScrollIndicator =false bottomView?.showsHorizontalScrollIndicator =false // 9.设置代理 bottomView?.delegate = self bottomView?.dataSource = self // 10.注册cell bottomView?.register(UICollectionViewCell().classForCoder, forCellWithReuseIdentifier:"ID"); if#available(iOS 10.0, *) { // 11.预加载 bottomView?.isPrefetchingEnabled =true }else{ // Fallback on earlier versions } } func setupTimer() { // 12.实例化定时器 timer = Timer.init(timeInterval: 2, target: self, selector: #selector(timerAction), userInfo: nil, repeats:true); RunLoop.main.add(timer!, forMode: RunLoopMode.defaultRunLoopMode);
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
self.timer?.fire(); } } func timerAction() {
var contentOffsetX = (self.bottomView?.contentOffset.x)! + self.frame.size.width
ifcontentOffsetX > self.frame.size.width * 3 { // 当前视图显示的是第三个的时候,设置bottomView的偏移量为0 self.bottomView?.contentOffset = CGPoint(x:0,y:0) contentOffsetX = self.frame.size.width } self.bottomView?.setContentOffset(CGPoint(x:contentOffsetX,y:0), animated:true) } // 重写removeFromSuperview方法,用于删除定时器,否则定时器一直存在,浪费内存 override func removeFromSuperview() {
timer?.invalidate() timer = nil super.removeFromSuperview() } // Mark:UICollectionViewDataSource // 设置Itmes func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return4; } // 设置cell func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell : UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier:"ID",for: indexPath) forview : UIView in cell.contentView.subviews {
view.removeFromSuperview() } let imageView = UIImageView.init(frame: cell.contentView.bounds) ifindexPath.row < 3 {
imageView.image = UIImage.init(named: String(indexPath.row))
}else{ imageView.image = UIImage.init(named: String(0)) } cell.contentView.addSubview(imageView)
returncell; } // Mark:UICollectionViewDelegate // 点击方法 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("您点击了第 \(indexPath.row == 3 ? 0 : indexPath.row) 个"); }
} |
UICollectionView 和 Timer的用法和OC基本相同。Swift和OC的UI部分应该是一致的,因为底层都是OpenGL。我直接说一下区别:
1.Timer:如果重复,OC是等一个间隔再执行的,Swift是立即执行的,所以我用了GCD延时开启定时器。
2.Swift 没有 CGPointZero。
无限轮播的原理就是在最后面多添加一个和第一个相同的itme。当你滑动到最后一个itme时,把UICollectionView的contentOffset置零,继续向右活动。如果不添加,会给用户一种卡顿的感觉。
本文给大家介绍了html+javascript+bootstrap实现层级多选框全层全选和多选功能的实现代码,非常有用,不回的小伙伴快来看看吧想做一个先按层级排序并可以多选的功能,首先倾向于用多层标签式的,直接选定加在文本域里, 查到这文,非常详细,如果大家需要做前面所说的功能,可以参考这个地址,http://mrthink.net/jquery-plugin-iselecttags/
但是,考虑到如果要做一个选项数不确定、可能非常多,用标签会容易视觉疲劳,同时要求随着选项数的变化,最小程度影响网页布局和效果,于是选择用层级的多选框,也就是设置电脑的那种常见效果。第二层选项可以折叠/展开到第一层下面,用bootstrap的 data-toggle="collapse" data-target="#demo1"
进行关联第二层折叠效果。
具体代码如下:
|
用Html实现人人网注册界面 ,Js实现生日栏表单三级联动
1. 效果
2.Html代码部分
代码如下 | 复制代码 |
<!DOCTYPE html> <html> <headlang="en"> <metacharset="UTF-8"> <title>人人网日期表单联动</title> <style> select { font:20px/40px '宋体'; } option {width: 100px;} </style> </head> <body> <formmethod="get"onchange="changeMonth()"> <b><h3align="center"> 免费开通人人网账号
</h3></b>
<tablealign="center"height="500">
<tr><td>注册邮箱: </td><td> <inputtype="text"name="name"/></td></tr> <tr><td> </td><td>你还可以使用 <ahref="https://www.baidu.com">账号</a> 注册或者 <ahref="https://www.baidu.com">手机号</a> 注册</td></tr>
<tr><td>创建密码: </td><td><inputtype="password"name="password"/></td></tr>
<tr><td>真实姓名: </td><td><inputtype="password"name="name"/></td>
<tr><tdalign="right">性别: </td><td>男<inputtype="radio"value="male"name="gender"/> 女<inputtype="radio"value="female"name="gender"/></td></tr>
<tr><tdalign="right">生日: </td><td>
<divid="box"> <selectname="sel1"id="sel1"> <optionvalue="year">年</option> </select> <selectname="sel2"id="sel2"> <optionvalue="month">月</option> </select> <selectname="sel3"id="sel3"> <optionvalue="day">日</option> </select> <spanid="result"></span> </div></td></tr>
<tr><tdalign="right">我现在: </td> <td><selectname="subject"> <optionvalue="xu">正在上学</option> <optionvalue="cz">工作</option> <optionvalue="gz">赋闲</option> <optionvalue="bk">经商</option> </select></td></tr>
<tr><td></td><td>![](verycode.gif) <ahref="https://www.baidu.com">看不清换一张?</a> </td></tr> <tr><td>验证码:</td><td><inputtype="password"name="yanzheng"/></td></tr> <tr><td></td><td><ahref="https://www.baidu.com"> ![](btn_reg.gif)</a></td></tr> </table> </form> </body> </html> |
3.JavaScript代码
代码如下 | 复制代码 |
<script> //生成日期 functioncreatDate() { //生成1900年-2100年 for(vari = 2016; i >= 1950; i--) { //创建select项 varoption = document.createElement('option'); option.setAttribute('value',i); option.innerHTML = i; sel1.appendChild(option); } //生成1月-12月 for(vari = 1; i <=12; i++){ varoption1 = document.createElement('option'); option1.setAttribute('value',i); option1.innerHTML = i; sel2.appendChild(option1); } //生成1日—31日 for(vari = 1; i <=31; i++){ varoption2 = document.createElement('option'); option2.setAttribute('value',i); option2.innerHTML = i; sel3.appendChild(option2); } } creatDate(); //保存某年某月的天数 vardays;
//年份点击 绑定函数 sel1.onclick =function() { //月份显示默认值 sel2.options[0].selected =true; //天数显示默认值 sel3.options[0].selected =true; } //月份点击 绑定函数 sel2.onclick =function() { //天数显示默认值 sel3.options[0].selected =true; //计算天数的显示范围 //如果是2月 if(sel2.value == 2) { //判断闰年 if((sel1.value % 4 === 0 && sel1.value % 100 !== 0) || sel1.value % 400 === 0) { days = 29; } else { days = 28; } //判断小月 }elseif(sel2.value == 4 || sel2.value == 6 ||sel2.value == 9 ||sel2.value == 11){ days = 30; }else{ days = 31; }
//增加或删除天数 //如果是28天,则删除29、30、31天(即使他们不存在也不报错) if(days == 28){ sel3.remove(31); sel3.remove(30); sel3.remove(29); } //如果是29天 if(days == 29){ sel3.remove(31); sel3.remove(30); //如果第29天不存在,则添加第29天 if(!sel3.options[29]){ sel3.add(newOption('29','29'),null) } } //如果是30天 if(days == 30){ sel3.remove(31); //如果第29天不存在,则添加第29天 if(!sel3.options[29]){ sel3.add(newOption('29','29'),null) } //如果第30天不存在,则添加第30天 if(!sel3.options[30]){ sel3.add(newOption('30','30'),null) } } //如果是31天 if(days == 31){ //如果第29天不存在,则添加第29天 if(!sel3.options[29]) { sel3.add(newOption('29','29'),null) } //如果第30天不存在,则添加第30天 if(!sel3.options[30]) { sel3.add(newOption('30','30'),null) } //如果第31天不存在,则添加第31天 if(!sel3.options[31]) { sel3.add(newOption('31','31'),null) } } }
//结果显示 设置好日期时间后 弹窗通知 box.onclick =function() { //当年、月、日都已经为设置值时 if(sel1.value !='year'&& sel2.value !='month'&& sel3.value !='day') { alert("日期时间已经设定好"); } }
</script> |
本文介绍了使用spring拦截器实现日志管理实例的教程,非常实用,有兴趣的同学可以参考一下本文
使用HandlerInterceptor拦截器,可以拦截请求,实现通用的日志管理操作
一、添加拦截器类
在"src/main/java"代码文件夹的"org.xs.demo1"的包下新建"LogInterceptor.java"类:
代码如下 | 复制代码 |
packageorg.xs.demo1;
importjava.text.SimpleDateFormat;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
importorg.springframework.core.NamedThreadLocal;
importorg.springframework.web.servlet.HandlerInterceptor;
importorg.springframework.web.servlet.ModelAndView;
/**
* 日志拦截器
* @author ThinkGem
*/
publicclassLogInterceptorimplementsHandlerInterceptor { privatefinalLogger log = LoggerFactory.getLogger(getClass().getName());
privatestaticfinalThreadLocal<Long> startTimeThreadLocal =newNamedThreadLocal<Long>("ThreadLocal StartTime"); /**
* 预处理
*/
@Override
publicbooleanpreHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throwsException {
longbeginTime = System.currentTimeMillis();//开始时间
startTimeThreadLocal.set(beginTime);//线程绑定变量(该数据只有当前请求的线程可见)
log.info("开始计时: {}",newSimpleDateFormat("hh:mm:ss.SSS").format(beginTime));
returntrue;
}
/**
* 返回处理
*/
@Override
publicvoidpostHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)throwsException {
if(modelAndView !=null){
log.info("ViewName: "+ modelAndView.getViewName());
}
}
/**
* 后处理
*/
@Override
publicvoidafterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throwsException {
// 保存日志
//LogUtils.saveLog(request, handler, ex, null);
// 输出日志信息
log.info("访问地址:"+ request.getRequestURI() +",执行方式:"+ request.getMethod());
longbeginTime = startTimeThreadLocal.get();//得到线程绑定的局部变量(开始时间)
longendTime = System.currentTimeMillis();//结束时间
log.info("计时结束:{}",newSimpleDateFormat("hh:mm:ss.SSS").format(endTime));
} } |
二、修改配置文件
修改spring-mvc.xml件,加入:
代码如下 | 复制代码 |
<!-- 拦截器配置 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mappingpath="/**"/>
<beanclass="org.xs.demo1.LogInterceptor"/>
</mvc:interceptor>
</mvc:interceptors> |
三、运行测试
访问"http://localhost:8080/demo1/hello/list2"地址
可以看到拦截器中输出的日志信息了