首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tvOS: UIMotionEffect指的是当滑动时没有反弹的UIButton

tvOS: UIMotionEffect指的是当滑动时没有反弹的UIButton
EN

Stack Overflow用户
提问于 2016-02-01 16:09:28
回答 1查看 333关注 0票数 0

我正在做一个注册页面,其中一个按钮是可聚焦的,我将UIMotionEffect应用于其中。

我的按钮声明:

代码语言:javascript
复制
private lazy var requestTokenButton: UIButton = {
    let button = UIButton()
    button.titleLabel?.font = UIFont(name: ThemeManager.sharedTheme.commonFontRegular, size: 22)
    button.setTitle("Get a New Code", forState: .Normal)
    button.setTitleColor(UIColor.blackColor(), forState: .Normal)
    button.backgroundColor = ThemeManager.sharedTheme.yellowColor
    button.addTarget(self, action: "requestNewToken", forControlEvents: .PrimaryActionTriggered)
    return button
}()

我使用Cartography进行编程自动布局:

代码语言:javascript
复制
constrain(self.requestTokenButton, self.registrationMoreInfoLabel) { view1, view2 in
    view1.leading == view2.leading
    view1.top == view2.bottom + 30
    view1.height == 65
    view1.width == 400
}

这是我的applyMotionEffect()

代码语言:javascript
复制
private func applyMotionEffect() {
    let yRotationMotionEffect = UIInterpolatingMotionEffect(keyPath: "layer.transform", type: .TiltAlongHorizontalAxis)
    let yMovementMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.y", type: .TiltAlongVerticalAxis)

    let m34 = CGFloat(-1.0 / 3000)
    let angle = CGFloat(20 * M_PI / 180.0)

    var baseTransform = CATransform3DIdentity
    baseTransform.m34 = m34

    var rotateYmin = baseTransform
    rotateYmin = CATransform3DRotate(rotateYmin, angle, 0.0, 1.0, 0.0);

    var rotateYmax = baseTransform
    rotateYmax = CATransform3DRotate(rotateYmax, -1 * angle, 0.0, 1.0, 0.0);

    yRotationMotionEffect.minimumRelativeValue = NSValue(CATransform3D: rotateYmin)
    yRotationMotionEffect.maximumRelativeValue = NSValue(CATransform3D: rotateYmax)


    self.requestTokenButton.addMotionEffect(yRotationMotionEffect)
    self.requestTokenButton.addMotionEffect(yMovementMotionEffect)
}

这是我的问题:当我从遥控器上滑动时,按钮在旋转到角度极限CGFloat(20 * M_PI / 180.0)后就会反弹回来。有没有一种方法可以旋转按钮,而不反弹回来,或减少旋转角度的数量,每点,我的手指移动电视遥控器。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-03 15:02:05

最后,我将UIImageView添加到UIButtonUIImageView.adjustsImageWhenAncestorFocused = true

需要提到的一点是,当只有一个UI元素是可聚焦的时,最好使用而不是

代码语言:javascript
复制
weak override var preferredFocusedView: UIView? 

这给了我奇怪的反弹效果,因为我只有一个可调的UI元素,所以焦点没有路可走。它总是调用preferredFocusedView并强制焦点回到UIButton的中心

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35135048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档