You need to enable JavaScript to run this app. Swift使用UISwipeGestureRecognizer滑动视图后如何在其移出屏幕时重新生成
优惠活动
大模型
产品
解决方案
定价
更多

Swift使用UISwipeGestureRecognizer滑动视图后如何在其移出屏幕时重新生成

问题分析

你现有代码的核心问题出在以下3点:

  • 动画的animations和completion参数需要传入闭包,你直接写赋值语句属于语法错误,无法按预期执行动画和结束后的复位逻辑
  • 硬编码的1000px偏移没有适配不同屏幕尺寸,无法保证视图完全移出屏幕
  • 你给swipeableView设置了translatesAutoresizingMaskIntoConstraints = false但没有添加约束,初始布局会异常

修正后完整实现代码

import UIKit

class ViewController: UIViewController {
    
    private let swipeableView: UIView = {
        let view = UIView()
        view.backgroundColor = .blue
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(swipeableView)
        // 添加全屏约束,保证视图覆盖整个屏幕
        NSLayoutConstraint.activate([
            swipeableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            swipeableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            swipeableView.topAnchor.constraint(equalTo: view.topAnchor),
            swipeableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
        
        // 添加滑动手势
        let directions: [UISwipeGestureRecognizer.Direction] = [.up, .down, .left, .right]
        directions.forEach { direction in
            swipeableView.addGestureRecognizer(createSwipeGestureRecognizer(for: direction))
        }
    }
    
    @objc private func didSwipe(_ sender: UISwipeGestureRecognizer) {
        let screenWidth = view.bounds.width
        let screenHeight = view.bounds.height
        var targetFrame = swipeableView.frame
        
        // 根据滑动方向计算移出屏幕的目标位置
        switch sender.direction {
        case .up:
            targetFrame.origin.y = -screenHeight
        case .down:
            targetFrame.origin.y = screenHeight
        case .left:
            targetFrame.origin.x = -screenWidth
        case .right:
            targetFrame.origin.x = screenWidth
        default:
            break
        }
        
        // 执行滑出动画,动画结束后立刻复位视图到初始位置
        UIView.animate(withDuration: 0.25) {
            self.swipeableView.frame = targetFrame
        } completion: { _ in
            // 复位到初始全屏位置,不需要重新创建视图,直接改frame即可达到和重新生成完全相同的效果,性能更好
            self.swipeableView.frame = self.view.bounds
        }
    }
    
    private func createSwipeGestureRecognizer(for direction: UISwipeGestureRecognizer.Direction) -> UISwipeGestureRecognizer {
        let swipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(didSwipe(_:)))
        swipeGestureRecognizer.direction = direction
        return swipeGestureRecognizer
    }
}

关键修改说明

  • 给swipeableView添加了全屏约束,保证在所有设备上都能正确覆盖全屏
  • 滑动目标偏移改为用当前屏幕的宽高计算,保证不管什么设备都能让视图完全移出屏幕
  • 修正了动画的写法,用闭包包裹动画执行逻辑和结束后的复位逻辑,不需要重新创建视图,直接复位frame即可达到和重新生成完全相同的效果,性能更优
  • 优化了手势添加的代码,用数组遍历减少重复代码

内容的提问来源于stack exchange,提问作者jecbon craig

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 00:21:03