【问题标题】:Swift - PanGestureRecognizer with Hero animationSwift - 带有英雄动画的 PanGestureRecognizer
【发布时间】:2020-01-09 22:50:14
【问题描述】:

我正在尝试使用 Hero-Framework 实现 Tutorial 中解释的动画。

目前我处于教程中的第 3 步,用户应该能够拖动视图。但是,这对我不起作用。这是我的代码:

// define a small helper function to add two CGPoints
func addCGPoints (left: CGPoint, right: CGPoint) -> CGPoint {
  return CGPoint(x: left.x + right.x, y: left.y + right.y)
}

// handle swqipe down gesture
@objc private func handlePan(gestureRecognizer:UIPanGestureRecognizer) {
  switch panGR.state {
  case .began:
    // begin the transition as normal
    dismiss(animated: true, completion: nil)
  case .changed:
    // calculate the progress based on how far the user moved
    let translation = panGR.translation(in: nil)
    let progress = translation.y / 2 / view.bounds.height
    Hero.shared.update(progress)

    // update views' position based on the translation
    Hero.shared.apply(modifiers: [.position(addCGPoints(left: translation, right: self.wishlistBackgroundView.center))], to: self.wishlistBackgroundView)
    Hero.shared.apply(modifiers: [.position(addCGPoints(left: translation, right: self.dismissWishlistViewButton.center))], to: self.dismissWishlistViewButton)
    Hero.shared.apply(modifiers: [.position(addCGPoints(left: translation, right: self.menueButton.center))], to: self.menueButton)
  default:
    // end the transition when user ended their touch
    Hero.shared.finish()
  }
}

这就是我设置ViewController的方式:

view.addSubview(wishlistBackgroundView)
    view.addSubview(dismissWishlistViewButton)
    view.addSubview(menueButton)
    wishlistBackgroundView.addSubview(wishlistView)
    wishlistView.addSubview(wishlistLabel)
    wishlistView.addSubview(wishlistImage)
    wishlistView.addSubview(theTableView.tableView)
    wishlistView.addSubview(addWishButton)

目前视图不可拖动,如教程中所示。 步骤 1步骤 2 工作得非常好,但不知何故 根据翻译更新视图位置 对我不起作用。

有谁知道我做错了什么?如果有更多上下文可以解释,请告诉我:)

【问题讨论】:

    标签: ios swift animation uipangesturerecognizer


    【解决方案1】:

    通过添加这一行来修复它:

    self.wishlistBackgroundView.hero.modifiers = [.fade, .translate(CGPoint(x: 0, y: 800), z: 20)]
    

    【讨论】:

      猜你喜欢
      • 2017-11-08
      • 2021-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-03
      • 1970-01-01
      • 2019-09-28
      相关资源
      最近更新 更多