专业java、php、iOS、C++、网页设计、平面设计、网络营销、游戏开发、前端与移动开发培训机构

新特性界面完成

  • 1.在 viewDidLoad 里面设置layout的参数

    override func viewDidLoad() {
      super.viewDidLoad()
    
      // 注册cell
      self.collectionView!.registerClass(CZNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    
      // 准备layout
      prepareLayout()
    }
    
    /**
    准备CollectionView 的layout
    */
    private func prepareLayout() {
      layout.itemSize = view.bounds.size
      layout.minimumInteritemSpacing = 0
      layout.minimumLineSpacing = 0
      layout.scrollDirection = UICollectionViewScrollDirection.horizontal
    
      collectionView?.isPagingEnabled = true
      collectionView?.bounces = false
      collectionView?.showsHorizontalScrollIndicator = false
    }
    
  • 添加开始按钮

    • 懒加载开始按钮

      /// 分享按钮
      lazy var shareButton: UIButton = {
        let button = UIButton()
      
        // 设置文字
        button.setTitle("分享给大家", for: UIControlState.normal)
        button.setTitleColor(UIColor.black, for: UIControlState.normal)
        button.setImage(UIImage(named: "new_feature_share_false"), for: UIControlState.normal)
        button.setImage(UIImage(named: "new_feature_share_true"), for: UIControlState.selected)
      
        button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
      
        // 点击事件
        button.addTarget(self, action: #selector(didClickShareButton), for: UIControlEvents.touchUpInside)
      
        return button
      }()
      
      /// 开始按钮
      lazy var startButton: UIButton = {
        let button = UIButton()
      
        // 设置文字
        button.setTitle("开始体验", for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), for: UIControlState.normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), for: UIControlState.highlighted)
      
        // 点击事件
        button.addTarget(self, action: #selector(didClickStartButton), for: UIControlEvents.touchUpInside)
      
        return button
      }()
      
    • 添加到cell的contentView里面
      // 1.添加控件
      contentView.addSubview(bkgImageView)
      contentView.addSubview(shareButton)
      contentView.addSubview(startButton)
      
    • 设置约束

      // MARK: - 准备UI
      private func setupUI() {
        // 添加为子控件
        contentView.addSubview(backgroundImageView)
        contentView.addSubview(shareButton)
        contentView.addSubview(startButton)
      
        // 添加约束
        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
        shareButton.translatesAutoresizingMaskIntoConstraints = false
        startButton.translatesAutoresizingMaskIntoConstraints = false
      
        // 背景
        // leading
        self.contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: self.contentView, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0))
        // top
        self.contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self.contentView, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0))
        // trailing
        self.contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: self.contentView, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0))
        // bottom
        self.contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.contentView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0))
      
        // 开始按钮
        // centerX
        self.contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self.contentView, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
        //
        self.contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.contentView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: -120))
      
        // 分享按钮
        // centerX
        self.contentView.addConstraint(NSLayoutConstraint(item: shareButton, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self.startButton, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
        // bottom
        self.contentView.addConstraint(NSLayoutConstraint(item: shareButton, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.startButton, attribute: NSLayoutAttribute.top, multiplier: 1, constant: -16))
      }
      
  • HMNewFeatureCell 定义 setButtonHidden: 来显示和隐藏 分享 开始 按钮

    class HMNewFeatureCell: UICollectionViewCell {
      ...
    
      func setButtonHidden(isHidden: Bool) -> Void {
          shareButton.isHidden = isHidden
          startButton.isHidden = isHidden
      }
    
      ...
    }
    
  • HMNewFeatureViewControllercollectionView 的数据源方法里面设置 分享按钮开始按钮 的显示和隐藏

    // 每个cell长什么样
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! HMNewFeatureCell
    
      // 设置当前cell是第几个cell
      cell.imageIndex = indexPath.item
    
      if indexPath.item == ItemCount - 1 {
          cell.setButtonHidden(isHidden: false)
      } else {
          cell.setButtonHidden(isHidden: true)
      }
    
      return cell
    }
    
  • 设置 分享按钮开始按钮 的点击事件
    // MARK: - 按钮点击事件
    /// 分享按钮被点击
    func didClickShareButton() {
      self.shareButton.selected = !self.shareButton.selected
    }
    
    /// 开始按钮被点击
    func didClickStartButton() {
      // TODO:切换到MainTabBarController
    }
    

results matching ""

    No results matching ""