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

使用SnapKit约束欢迎界面

  • 添加控件约束

    /// 准备控件
    func prepareUI() {
    
      // 添加控件
      view.addSubview(backgroundImageView)
      view.addSubview(iconView)
      view.addSubview(nameLabel)
    
      // 添加控件约束
      // 背景
      bkgImageView.snp_makeConstraints { (make) -> Void in
          make.edges.equalTo(self.view)
      }
    
      // 头像
      // centerX
      iconView.snp_makeConstraints { (make) -> Void in
          make.centerX.equalTo(self.view)
          make.bottom.equalTo(self.view).offset(-160)
          make.size.equalTo(CGSize(width: 85, height: 85))
      }
    
      // 欢迎label
      welcomeLabel.snp_makeConstraints { (make) -> Void in
          make.centerX.equalTo(iconView)
          make.top.equalTo(iconView.snp_bottom).offset(16)
      }
    }
    
  • 用户头像添加动画

      // MARK: - 动画
      // 动画在界面显示出来,我们看到界面的时候再做动画
      override func viewDidAppear(animated: Bool) {
          super.viewDidAppear(animated)
    
          moveAnimation()
      }
    
      /// 头像从下方移动到上方
      private func moveAnimation() {
          // 动画, 使用弹簧效果
          // usingSpringWithDamping: 范围 0 - 1之间, 数值越小弹簧效果月明显
          // initialSpringVelocity: 初始速度.
          UIView.animateWithDuration(1.2, delay: 0.1, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
              // snp_updateConstraints: 更新约束
              self.iconView.snp_updateConstraints(closure: { (make) -> Void in
                  // 更新约束不能修改参照,只能改变constant (offset)
                  make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.height - 160))
              })
    
              // AutoLayout动画必须调用layoutIfNeeded,layoutIfNeeded会根据约束重新设置frame
              self.view.layoutIfNeeded()
              }) { (_) -> Void in
                  // 移动动画完成, 是label.alpha = 1
                  UIView.animateWithDuration(1, animations: { () -> Void in
                      self.messageLabel.alpha = 1
                      }, completion: { (_) -> Void in
                          // TODO: 回到首页
                          print("动画完成")
                  })
          }
      }
    
  • usingSpringWithDamping 的范围为 0.0f1.0f,数值越小 弹簧 的振动效果越明显
  • initialSpringVelocity 则表示初始的速度,数值越大一开始移动越快

results matching ""

    No results matching ""