标签:
1 func requestData (type : Int , urlString : String , parameters : [ String : NSObject] , callBack : (result : AnyObject? , error : NSErroe?) -> () ) 2 func reqeustData(type : RequestType, urlString : String, parameters : [String : NSObject], finishedCallback : (result : AnyObject?, error : NSError?) -> ()) { }
guard let resultDict = result as? [String : NSObject] else { return } guard let dataArray = resultDict["data"] as? [[String : NSObject]] else { return }
// 最后一个cell已经出现 if indexPath.item == shops.count - 1 { indexPath.item 相当于 tableView 的 indexPath.row loadHomeData(shops.count) }
class func createBtn(title : String, bgColor : UIColor, fontSize : CGFloat) -> UIButton { let btn = UIButton() btn.backgroundColor = bgColor btn.setTitle(title, forState: .Normal) btn.titleLabel?.font = UIFont.systemFontOfSize(fontSize) return btn }
1 convenience init(title : String, bgColor : UIColor, fontSize : CGFloat) { 2 self.init() 3 4 setTitle(title, forState: .Normal) 5 backgroundColor = bgColor 6 titleLabel?.font = UIFont.systemFontOfSize(fontSize) 7 }
1 // MARK:- 重写构造函数 2 override init(frame: CGRect) { 3 super.init(frame: frame) 4 5 setupUI() 6 } 7 // required : 如果一个构造函数前有required,那么重写了其他构造函数时,那么该构造函数也必须被重写 8 required init?(coder aDecoder: NSCoder) { 9 fatalError("init(coder:) has not been implemented") 10 }
1 // 2.获取小图片 2 var smallImage = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(shop.q_pic_url) 3 if smallImage == nil { 4 smallImage = UIImage(named: "empty_picture") 5 }
collectionView.scrollToItemAtIndexPath(indexPath!, atScrollPosition: .CenteredHorizontally, animated: false)
1 // ?? : 先判断前面的可选链是否有值, 如果有值,解包并且获取对应类型的值. 如果没有值直接取后面的值 2 return shops?.count ?? 0
1 // 1.1.拿到正在显示的Cell 2 // visibleCells 返回所有在屏幕中显示的Cell 3 let cell = collectionView.visibleCells()[0] as! PhotoBrowserViewCell 4 guard let image = cell.imageView.image else { 5 return 6 }
1 // MARK:- 遵守转场的代理协议,和实现对应的方法 2 extension HomeViewController : UIViewControllerTransitioningDelegate { 3 // 为弹出控制器做一个动画 4 func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? { 5 //记录当前为显示阶段 6 isPresented = true 7 return self 8 } 9 10 // 为消失控制器做一个动画 11 func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { 12 //记录当前为消失阶段 13 isPresented = false 14 return self 15 } 16 } 17 18 extension HomeViewController : UIViewControllerAnimatedTransitioning { 19 // 返回动画执行的时间 20 func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval { 21 return 3 22 } 23 24 // transitionContext : 转场上下文 25 // 作用 : 可以通过上下文获取到弹出的View和消失的View 26 // UITransitionContextFromViewKey : 获取消失的View 27 // UITransitionContextToViewKey : 获取弹出的View 28 func animateTransition(transitionContext: UIViewControllerContextTransitioning) { 29 if isPresented { 30 // 获取弹出的View 31 let presentedView = transitionContext.viewForKey(UITransitionContextToViewKey)! 32 //需要把view添加到父控件上,才能有动画效果 33 //父控件就是widow的containerView, 通过transitionContext.containerView()拿到 34 transitionContext.containerView()?.addSubview(presentedView) 35 36 // 修改View alpha值 37 presentedView.alpha = 0.0 38 39 // 执行动画 40 UIView.animateWithDuration(transitionDuration(transitionContext), animations: { 41 presentedView.alpha = 1.0 42 }) { (isFinished : Bool) in 43 //告诉控制器,转场动画完成 44 transitionContext.completeTransition(isFinished) 45 } 46 } else { 47 // 1.获取消失的View 48 let dismissedView = transitionContext.viewForKey(UITransitionContextFromViewKey)! 49 50 // 2.执行动画 51 UIView.animateWithDuration(transitionDuration(transitionContext), animations: { 52 dismissedView.alpha = 0.0 53 }, completion: { (isFinished : Bool) in 54 //移除view,显示主控制器的view 55 dismissedView.removeFromSuperview() 56 transitionContext.completeTransition(isFinished) 57 }) 58 } 59 } 60 }
swift项目初体验--教你打造一款个性化图片浏览器(篇幅过大,慎入)
标签:
原文地址:http://www.cnblogs.com/xiaotian666/p/5799040.html