gif.gif

gif 可以右键在新的标签页中打开查看

简单实现类似这种的 滑动菜单 新手Swift新手 大神就别看了

import UIKit

class HomeViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,uiscrollviewdelegate {

    let WIDTH = UIScreen.mainScreen().bounds.width / 375 let HEIGTH = UIScreen.mainScreen().bounds.height / 667 let SCREENWIDTH = UIScreen.mainScreen().bounds.width let SCREENHEIGTH = UIScreen.mainScreen().bounds.height var collectionView: UICollectionView!
    var buttonsArray: NSMutableArray!
    var titlesArray: NSMutableArray!
    var lineView: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()

        self.automaticallyAdjustsScrollViewInsets = false
        self.navigationController?.automaticallyAdjustsScrollViewInsets = false

        self.initButtons()
        self.initCollectionView()
    }

    func initButtons() {

        buttonsArray = NSMutableArray()
        titlesArray = ["分类","品牌","首页","专题","品牌"]

        for var i = 0;i<titlesArray.count;i++ { let button = UIButton.init(type: .System)
            button.frame = CGRectMake(SCREENWIDTH * CGFloat (i) / 5.0,SCREENWIDTH / 5.0,40 * HEIGTH)
            button.tag = i
            button.backgroundColor = UIColor.blackColor()
            button.setTitleColor(UIColor.lightGrayColor(),forState: .normal)
            if i == 2 {

            button.titleLabel?.font = UIFont.systemFontOfSize(16.0 * WIDTH)
            button.whiteColor(),0)">.normal)

            }
            button.systemFontOfSize(15.0 * WIDTH)
            button.setTitle(titlesArray[i] as? String,0)">.normal)
            button.addTarget(self,action: "click:",forControlEvents: .TouchUpInside)
            self.view.addSubview(button)
            buttonsArray.addobject(button)

        }
        lineView = UIView.init(frame: CGRectMake(15 * WIDTH + (SCREENWIDTH / 5.0) * 2,38 * HEIGTH,SCREENWIDTH / 5.0 - 30 * WIDTH,2 * HEIGTH))
        lineView.whiteColor()
        self.addSubview(lineView)

    }
    func click (btn: UIButton) {

        for btt in buttonsArray {

            btt.titleLabel?!.systemFontOfSize(15)
            btt.normal)
        }

        btn.systemFontOfSize(16 * WIDTH)
        btn.normal)
        self.collectionView.scrollToItemAtIndexPath(NSIndexPath.init(forItem: btn.tag,inSection: 0),atScrollPosition: .None,animated: true)

        UIView.animateWithDuration(0.3,animations: { () -> Void in

            self.lineView.frame = CGRectMake(15 * WIDTH + (UIScreen.mainScreen().bounds.width / 5.0) * CGFloat (btn.tag),UIScreen.width / 5.0 - 30 * WIDTH,2)

            }) {(Bool) -> Void in

            }
    }

    func initCollectionView() {

        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = .Horizontal
        flowLayout.itemSize = CGSizeMake(UIScreen.width,0)">.height - 88 * HEIGTH)
        flowLayout.minimumLinespacing = 0
        flowLayout.minimumInteritemSpacing = 0

        collectionView = UICollectionView.init(frame: CGRectMake(0,40 * HEIGTH,0)">.height - 88 * HEIGTH),collectionViewLayout: flowLayout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.pagingEnabled = true
        collectionView.init(forRow: 2,animated: true)
        self.addSubview(collectionView)
        collectionView.registerClass(HomeCollectionViewCell.classForKeyedArchiver(),forCellWithReuseIdentifier: "hCell")
        collectionView.registerClass(KindCollectionViewCell.registerClass(BrandCollectionViewCellbcell")
        collectionView.registerClass(SubjectCollectionViewCell.registerClass(GiftCollectionViewCell.item == 0 {
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier("kCell",forIndexPath: indexPath) as! KindCollectionViewCell

            weak var weakSelf = self
            cell.initClosure({ (str) -> Void? in

//                let url = "http://mobile.iliangcang.com/goods/goodsShare?a=b&page=1&count=10&coverId=1&cat_code=$&app_key=iPhone&v=3.0.0&sig=97E9576F-B96F-48FD-BE0E-CD84610BC975"

                let common = CommonViewController()
//                common.urlStr = url.stringByReplacingOccurrencesOfString("$",withString: str)
                common.urlStr = str
                common.hidesBottomBarWhenPushed = true
                weakSelf!.pushViewController(common,animated: true)
                return nil
             })

            return cell

        }else if indexPath.item == 1 {
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier("bcell",forIndexPath: indexPath) as! BrandCollectionViewCell
            return cell
        }else if indexPath.item == 2 {
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier("hCell",forIndexPath: indexPath) as! HomeCollectionViewCell
            cell.myClosure = { (content_id: String) -> Void in

                let shopVC = ShopViewController()
                let url = "http://mobile.com/goods/goodsList?a=b&list_id=$&count=10&page=1&app_key=iPhone&v=3.0.0&sig=97E9576F-B96F-48FD-BE0E-CD84610BC975"
                shopVC.pushViewController(shopVC,animated: true)
            }
            return cell
        }else if indexPath.item == 3 {

            let cell = collectionView.dequeueReusableCellWithReuseIdentifier("sCell",forIndexPath: indexPath) as! SubjectCollectionViewCell

            return cell

        }else {

            let cell = collectionView.dequeueReusableCellWithReuseIdentifier("gCell",forIndexPath: indexPath) as! GiftCollectionViewCell

            return cell

        }

    }

    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

        for btt in buttonsArray {

            btt.normal)
        }

        UIView.animateWithDuration(0.3) { () -> Void in

        self.width / 5.0) * CGFloat (self.contentOffset.x / (UIScreen.width)),2)
        }

        let btn = buttonsArray.objectAtIndex(Int(self.x / UIScreen.width))

        btn.normal)

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // dispose of any resources that can be recreated.
    }

}

Swift 类似网易新闻的滑动菜单的更多相关文章

  1. HTML5 input新增type属性color颜色拾取器的实例代码

    type 属性规定 input 元素的类型。本文较详细的给大家介绍了HTML5 input新增type属性color颜色拾取器的实例代码,感兴趣的朋友跟随脚本之家小编一起看看吧

  2. amazeui模态框弹出后立马消失并刷新页面

    这篇文章主要介绍了amazeui模态框弹出后立马消失并刷新页面,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  3. 移动HTML5前端框架—MUI的使用

    这篇文章主要介绍了移动HTML5前端框架—MUI的使用的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  4. AmazeUI 模态窗口的实现代码

    这篇文章主要介绍了AmazeUI 模态窗口的实现代码,代码简单易懂,非常不错,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  5. ios – UIPopoverController出现在错误的位置

    所以我花了一些时间寻找答案,但到目前为止还没有找到任何答案.我正在尝试从UIInputAccessoryView上的按钮呈现弹出窗口.UIBarButtonItem我想显示popover来自定制视图,所以我可以使用图像.我创建这样的按钮:当需要显示popover时,我这样做:但我得到的是:弹出窗口看起来很好,但它应该出现在第一个按钮上时出现在第二个按钮上.然后我发现了这个问题:UIBarButto

  6. ios – 关闭UIBarButtonItem上的突出显示

    我正在尝试使用UIBarButtonItem在我的UIToolbar上添加标题.我使用简单的风格,看起来很好,但我似乎无法让它停止突出显示触摸.“突出显示时触摸”选项不适用于条形按钮项目.有没有快速简便的方法来做到这一点?

  7. 以编程方式调整iOS中的按钮大小

    我正在使用XCode4.6.1并开发iOS6.我在故事板上添加了一个按钮.我在我的实现文件ViewController.m中创建了一个插座:我尝试按如下所示更改按钮b1的属性(在同一个文件中:ViewController.m):当我在模拟器中运行应用程序时,按钮的alpha成功设置为0.5.但是,按钮的位置和大小不会改变.我尝试了各种方法来实现它.然而似乎没有任何作用.我想知道我做错了什么.我对O

  8. 如何在iOS / Swift的顶部导航栏中添加“继续”按钮

    我想在导航栏的右侧添加一个“继续”按钮.如何实现这一目标?我一直在尝试使用UIBarButtonItem上的一些方法,但无法使其正常工作.我迄今为止的最大努力是:但我在第一行遇到错误.它不喜欢“style”参数.我也试过了但没有运气.仍然停留在样式参数上.有任何想法吗?

  9. ios – 将图像添加到界面构建器中的按钮

    我想在我的按钮而不是文本中添加图像.我可以在界面构建器中这样做吗?我可以看一下这个例子吗?

  10. ios – 自定义BackBarButtonItem

    主要原因是您丢失了标准后退按钮滑动动画以更改视图.此外,这意味着我不需要使用自定义按钮或编写自定义方法返回.它只是起作用.希望这也能解决别人的问题,我知道我已经坚持了3个小时!

随机推荐

  1. Swift UITextField,UITextView,UISegmentedControl,UISwitch

    下面我们通过一个demo来简单的实现下这些控件的功能.首先,我们拖将这几个控件拖到storyboard,并关联上相应的属性和动作.如图:关联上属性和动作后,看看实现的代码:

  2. swift UISlider,UIStepper

    我们用两个label来显示slider和stepper的值.再用张图片来显示改变stepper值的效果.首先,这三个控件需要全局变量声明如下然后,我们对所有的控件做个简单的布局:最后,当slider的值改变时,我们用一个label来显示值的变化,同样,用另一个label来显示stepper值的变化,并改变图片的大小:实现效果如下:

  3. preferredFontForTextStyle字体设置之更改

    即:

  4. Swift没有异常处理,遇到功能性错误怎么办?

    本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请发送邮件至dio@foxmail.com举报,一经查实,本站将立刻删除。

  5. 字典实战和UIKit初探

    ios中数组和字典的应用Applicationschedule类别子项类别名称优先级数据包contactsentertainment接触UIKit学习用Swift调用CocoaTouchimportUIKitletcolors=[]varbackView=UIView(frame:CGRectMake(0.0,0.0,320.0,CGFloat(colors.count*50)))backView

  6. swift语言IOS8开发战记21 Core Data2

    上一话中我们简单地介绍了一些coredata的基本知识,这一话我们通过编程来实现coredata的使用。还记得我们在coredata中定义的那个Model么,上面这段代码会加载这个Model。定义完方法之后,我们对coredata的准备都已经完成了。最后强调一点,coredata并不是数据库,它只是一个框架,协助我们进行数据库操作,它并不关心我们把数据存到哪里。

  7. swift语言IOS8开发战记22 Core Data3

    上一话我们定义了与coredata有关的变量和方法,做足了准备工作,这一话我们来试试能不能成功。首先打开上一话中生成的Info类,在其中引用头文件的地方添加一个@objc,不然后面会报错,我也不知道为什么。

  8. swift实战小程序1天气预报

    在有一定swift基础的情况下,让我们来做一些小程序练练手,今天来试试做一个简单地天气预报。然后在btnpressed方法中依旧增加loadWeather方法.在loadWeather方法中加上信息的显示语句:运行一下看看效果,如图:虽然显示出来了,但是我们的text是可编辑状态的,在storyboard中勾选Editable,再次运行:大功告成,而且现在每次单击按钮,就会重新请求天气情况,大家也来试试吧。

  9. 【iOS学习01】swift ? and !  的学习

    如果不初始化就会报错。

  10. swift语言IOS8开发战记23 Core Data4

    接着我们需要把我们的Rest类变成一个被coredata管理的类,点开Rest类,作如下修改:关键字@NSManaged的作用是与实体中对应的属性通信,BinaryData对应的类型是NSData,CoreData没有布尔属性,只能用0和1来区分。进行如下操作,输入类名:建立好之后因为我们之前写的代码有些地方并不适用于coredata,所以编译器会报错,现在来一一解决。

返回
顶部