- vừa được xem lúc

Custom NavigationController animation

0 0 19

Người đăng: Trịnh Giang Đông

Theo Viblo Asia

Khởi tạo interactive:

Trong NavigationController, khai báo interactionControlleredgeSwipeGestureRecognizer

private var interactionController: UIPercentDrivenInteractiveTransition?
private var edgeSwipeGestureRecognizer: UIScreenEdgePanGestureRecognizer?

Config các Gesture trong viewDidLoad()

edgeSwipeGestureRecognizer = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(handleSwipe(_:)))
edgeSwipeGestureRecognizer!.edges = .left
view.addGestureRecognizer(edgeSwipeGestureRecognizer!)

Tạo hàm để hanle các gesture event

@objc func handleSwipe(_ gestureRecognizer: UIScreenEdgePanGestureRecognizer) { // 1 let percent = gestureRecognizer.translation(in: gestureRecognizer.view!).x / gestureRecognizer.view!.bounds.size.width if gestureRecognizer.state == .began { // 2 interactionController = UIPercentDrivenInteractiveTransition() popViewController(animated: true) } else if gestureRecognizer.state == .changed { // 3 interactionController?.update(percent) } else if gestureRecognizer.state == .ended { // 4 if percent > 0.2 && gestureRecognizer.state != .cancelled { interactionController?.finish() } else { interactionController?.cancel() } interactionController = nil }
}

// 1: Đầu tiên, tính toán khoảng cách mà người dùng đã vuốt. // 2: Nếu cử chỉ mới bắt đầu, tạo interactionController, sau đó bắt đầu quá trình chuyển đổi bằng cách gọi popViewController(animated:). // 3: Bất cứ khi nào tiến trình thay đổi, gọi interactionController?.update(percent). // 4: Khi tác hoàn tất, yêu cầu interactionController kết thúc quá trình chuyển đổi hoặc hủy nó, với các phương thức finish()cancel() tương ứng. Cuối cùng, set interactionController trở lại nil.

Sử dụng interactionController trong delegate

func navigationController(_ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? { return interactionController
}

Bình luận

Bài viết tương tự

- vừa được xem lúc

Swift: Tạo custom phép toán tử (Operator) của riêng bạn!

Swift cho phép bạn tạo các toán tử có thể tùy chỉnh của riêng bạn. Điều này đặc biệt hữu ích khi bạn xử lý các loại dữ liệu của riêng mình. Operator Types in Swift. Có năm loại toán tử chính trong Swift.

0 0 45

- vừa được xem lúc

Code ngắn gọn hơn với OptionSet trong Swift

. Nếu bạn muốn biết cách xử lý với Bitmasks trong Swift hay là bạn đã từng nghe đến OptionSet chưa? Bài viết này sẽ giúp bạn hiểu hơn về điều đó . 1.

0 0 26

- vừa được xem lúc

Chương 6 Protocol oriented programming.

Cuốn sách này là về lập trình hướng protocol. Khi Apple thông báo swift 2 ở WWDC 2015.

0 0 28

- vừa được xem lúc

Ví dụ về UIActivityViewController

Trên iOS, UIActivityViewController cung cấp giao diện thống nhất để người dùng chia sẻ và thực hiện các hành động trên văn bản, hình ảnh, URL và các mục khác trong ứng dụng. let string = "Hello, world!". let url = URL(string: "https://nshipster.com").

0 0 45

- vừa được xem lúc

Quản lý self và cancellable trong Combine.

. . Công việc quản lý bộ nhớ memory management thường trở nên phức tạp khi chúng ta thực hiện các tác vụ bất đồng bộ asynchronous vì chúng ta thường phải lưu giữ một số object nằm ngoài scope mà object được define trong khi vẫn phải đảm bảo được việc giải phóng object đó được thực hiện đúng quy trìn

0 0 28

- vừa được xem lúc

[SWIFT] Sử dụng Dependency Injection với Storyboards

1. Lời mở đầu:.

0 0 30