ios - 图像移过后按钮会改变吗?

标签 ios swift xcode

我正在制作一个“滑动确认”按钮,设置是自定义 UIButton + 常规 UIImageView,图像被传递到按钮,按钮监听触摸并相应地移动图像。 一切都很好,除了按钮释放后的一个细节外,一切都应该回到初始状态,但按钮保持变化,就好像它的 alpha 减半一样

before after

我评论了 alpha,因为该按钮将要隐藏,但是一旦我遇到这个问题,我就禁用了它,无论是否设置 alpha,问题都会发生。

这是自定义按钮的代码

class SlideButton: UIButton {

    var arrowImage: UIImageView?
    var initialLocation: CGPoint?
    var initialArrowPosition: CGPoint?

    func setArrow(arrow:UIImageView){
        arrowImage = arrow
        initialArrowPosition = arrow.frame.origin
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        let touch = touches.first!
        let location = touch.location(in: self.superview)
        initialLocation = location
        UIView.animate(withDuration: 0.2) {
            //self.alpha = 0.0
          }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        UIView.animate(withDuration: 0.3) {
            self.arrowImage!.frame.origin = self.initialArrowPosition!
            //self.alpha = 1.0
        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first!
        let currentLocation = touch.location(in: self.superview)
        arrowImage!.frame.origin.x = initialArrowPosition!.x - initialLocation!.x + (currentLocation.x)
    }
 }

最佳答案

您要记得为您的touches 方法之一调用super:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesBegan(touches, with: event)

但对于其他 touches 方法,您忘记了这样做,因此您破坏了按钮的核心功能。

关于ios - 图像移过后按钮会改变吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58564068/

相关文章:

iOS:架构 x86_64 的 221 个重复符号 clang:错误:链接器命令失败,退出代码 1

ios - 使用 NSUserDefaults 持久保留 UITableView 选定单元格的 UIAccessoryType

ios - [_UIWebViewScrollViewDelegateForwarder scrollViewWasRemoved :]: unrecognized selector sent to instance

ios - 如何将多个观察者绑定(bind)到一个 ControlProperty

iOS 在 didSelectItemAtIndexPath 处展开 CollectionView

ios - 如何重命名 iCloud 容器?

ios - 如何在ARC中集成ASIHTTPRequest

ios - 链接器命令在我的 Swift 应用程序上失败,退出代码为 1

swift - Facebook 登录不会在登录后关闭登录对话框

c++ - 动态链接器 - 如何找到丢失的符号