mapanguan

导航

Swift可向上滑移出界面的欢迎页简单封装

使用:
-(WelcomView*)welcomeView{
    if (!_welcomeView) {
        _welcomeView = [[NSBundle mainBundle] loadNibNamed:@"WelcomView" owner:nil options:nil].firstObject;
        _welcomeView.frame = self.window.frame;
    }
    return _welcomeView;
}

............
         ImgModel *im = [[ImgModel alloc]init];
          [im setValuesForKeysWithDictionary:obj];
          self.welcomeView.imgArray = @[im];    

 


//MARK:- Timer extension Timer{ /** *计时器暂停 */ func timer_pause(){ self.fireDate = Date.distantFuture } /** *计时器继续 */ func timer_continue(){ self.fireDate = Date.distantPast } }

import UIKit //图片信息模型,根据需求修改 class ImgModel: NSObject { var PicPath = "" var PicDescription = "" var AppSubjectPictureID = "" var InfoLinkUrl = "" var ShowSeconds = "" override func setValue(_ value: Any?, forUndefinedKey key: String) { #if DEBUG print("ImgModel出现了未定义的字段:\(key)-->\(String(describing: value))") #endif } } class WelcomView: UIView ,UIScrollViewDelegate{ //空间关联 @IBOutlet weak var scrollView: UIScrollView! @IBOutlet weak var pageController: UIPageControl! @IBOutlet weak var titleLB: UILabel! @IBOutlet weak var btnBgview: UIView! @IBOutlet weak var timeLB: UILabel! @IBOutlet weak var intoMainBtn: UIButton! var stopIndex = 0 //滚动视图停止时候停在的位置 var beginPoint = CGPoint() var beginPoint1 = CGPoint() var second = 0 var beginSecond = 0 var timer:Timer? //图片资源数组 var imgArray = [ImgModel]() { didSet{ scrollView.tag = 1000 scrollView.contentSize = CGSize(width: CGFloat(imgArray.count)*self.frame.width, height: self.frame.height) pageController.numberOfPages = imgArray.count scrollView.isPagingEnabled = true scrollView.bounces = true scrollView.showsVerticalScrollIndicator = false scrollView.showsHorizontalScrollIndicator = false scrollView.delegate = self for (index,ele) in imgArray.enumerated() { let sv = UIScrollView(frame: CGRect(x: CGFloat(index)*self.frame.width, y: 0, width: self.frame.width, height: self.frame.height)) sv.tag = 1000 + (index + 1) sv.delegate = self sv.bounces = false sv.showsVerticalScrollIndicator = false sv.showsHorizontalScrollIndicator = false sv.decelerationRate = 0.5 sv.contentSize = CGSize(width: self.frame.width, height: self.frame.height*2) let imgView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)) sv.addSubview(imgView) scrollView.addSubview(sv) imgView.isUserInteractionEnabled = true let tap = UITapGestureRecognizer(target: self, action:#selector(tapAction)) imgView.addGestureRecognizer(tap) second = Int(ele.ShowSeconds)! beginSecond = second DispatchQueue.main.async { imgView.sd_setImage(with: URL(string:ele.PicPath), placeholderImage: UIImage(named: "正在加载"), options: .lowPriority, completed: { (img:UIImage?, erro:Error?, type:SDImageCacheType, imgUrl:URL?) in if erro != nil{ self.removeFromSuperview() self.showHint((erro?.localizedDescription)!) }else{ appDelegate.window.addSubview(self) } }) } } timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerNumChange(t:)), userInfo: nil, repeats: true) //self.perform(#selector(removeSelf), with: nil, afterDelay: TimeInterval(second)) self.bringSubview(toFront: self.btnBgview) } } func timerNumChange(t:Timer){ second -= 1 if second >= 0 { timeLB.text = "\(second)s" }else{ self.removeSelf() timer?.invalidate() } } @IBAction func btnAction(_ sender: UIButton) { self.removeFromSuperview() } //将自己移出界面 func removeSelf(){ UIView.animate(withDuration: 0.5, animations: { self.center.y = -sheight/2 }) { (done:Bool) in if done { self.removeFromSuperview() } } } override func draw(_ rect: CGRect) { // Drawing code super.draw(rect) } override func awakeFromNib() { super.awakeFromNib() btnBgview.layer.cornerRadius = 25/2 btnBgview.layer.masksToBounds = true } //图片点击事件 func tapAction(){ print("点击了第\(stopIndex)张") } //MARK:ScrollView delegate func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { if scrollView.tag > 1000 { beginPoint = self.center beginPoint1 = scrollView.contentOffset timer?.timer_pause() //NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(removeSelf), object: nil) } } func scrollViewDidScroll(_ scrollView: UIScrollView) { if scrollView.tag > 1000 { self.center.y = beginPoint.y - scrollView.contentOffset.y - beginPoint1.y for imgView in scrollView.subviews { if imgView.isKind(of: UIImageView.self) { imgView.center = CGPoint(x: appDelegate.window.center.x, y: scrollView.center.y + scrollView.contentOffset.y - beginPoint1.y) break } } } } func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { if scrollView.tag == 1000 { let offset = scrollView.contentOffset.x let swidth = UIScreen.main.bounds.size.width stopIndex = Int(offset / swidth) pageController.currentPage = stopIndex }else{ scrollView.isScrollEnabled = true scrollViewDidEndAction(scrollView) } } func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { if decelerate{ if scrollView.tag > 1000 { scrollView.isScrollEnabled = false } }else{ if scrollView.tag > 1000 { scrollViewDidEndAction(scrollView) } } } //MARK:滚动结束(界面速度为0) func scrollViewDidEndAction(_ scrollView: UIScrollView){ if self.center.y < 0 { UIView.animate(withDuration: 0.5, animations: { self.center = CGPoint(x: self.center.x, y: appDelegate.window.center.y * -2) }) { (done:Bool) in if done { self.removeFromSuperview() } } }else{ UIView.animate(withDuration: 0.3, delay: 0.1, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.1, options: .curveLinear, animations: { self.center = appDelegate.window.center }) { (done:Bool) in if done { scrollView.contentOffset = self.beginPoint1 for imgView in scrollView.subviews { if imgView.isKind(of: UIImageView.self) { imgView.center = CGPoint(x: appDelegate.window.center.x, y: scrollView.center.y) break } } self.second = self.beginSecond + 1 self.timer?.timer_continue() } } } } }

 

XIB效果图 

posted on 2017-07-26 17:24  mapanguan  阅读(533)  评论(0编辑  收藏  举报