提问人:Karina 提问时间:5/16/2023 更新时间:5/16/2023 访问量:32
Swift GestureRecognizer
Swift GestureRecognizer
问:
我的任务是: 实现到显示 JSON 文件中数据的模式屏幕的过渡。 屏幕上的 GIF 可以正确加载并正常工作,但过渡到模态屏幕则无法正常工作。你能帮帮我吗? 我已经添加了过渡,但由于某种原因,它们不起作用。 模态屏幕还应包含移动的 GIF。 换句话说,每个GIF都应该对应一个特定的练习。 我应该注意我的JSON文件更大;我提供了一小部分作为示例。
JSON的
{
"exercises": [
{
"url": "https://www.fitnesschemas.nl/images/v2/dumbell%20row.gif",
"name": "text",
"description": "text",
"benefits": "text"
}
]
}
身体锻炼视图控制器
import UIKit
import WebKit
struct ExerciseBody: Codable {
let url: String
}
class BodyExercisesViewController: UIViewController {
var exercises: [ExerciseBody] = []
var imageViews: [UIImageView] = []
let scrollView = UIScrollView()
override func viewDidLoad() {
super.viewDidLoad()
let backButton = UIBarButtonItem(title: "Назад", style: .plain, target: nil, action: nil)
navigationItem.backBarButtonItem = backButton
view.backgroundColor = UIColor(named: "Background")
scrollView.frame = view.bounds
scrollView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(scrollView)
let label = UILabel(frame: CGRect(x: 20, y: 20, width: view.bounds.width - 40, height: 50))
label.text = "Всё тело"
label.textAlignment = .left
label.textColor = UIColor(named: "Green")
label.font = UIFont.boldSystemFont(ofSize: 40)
scrollView.addSubview(label)
let button = UIButton(frame: CGRect(x: 20, y: scrollView.contentSize.height + 875, width: view.bounds.width - 40, height: 50))
button.backgroundColor = UIColor(named: "Blue")
button.layer.cornerRadius = 20
button.setTitle("Старт", for: .normal)
button.setTitleColor(.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 25)
scrollView.addSubview(button)
loadExercises()
}
private func loadExercises() {
guard let url = Bundle.main.url(forResource: "allBody", withExtension: "json") else {
print("JSON file not found")
return
}
do {
let data = try Data(contentsOf: url)
let decoder = JSONDecoder()
exercises = try decoder.decode([String: [ExerciseBody]].self, from: data)["exercises"] ?? []
print("Data loaded successfully!")
setupImageViews()
} catch {
print("Error loading data: \(error)")
}
}
private func setupImageViews() {
let screenWidth = UIScreen.main.bounds.width
let spacing: CGFloat = 10
let rectangleWidth = (screenWidth - spacing * 3) / 2
var xPosition: CGFloat = spacing
var yPosition: CGFloat = 100
let group = DispatchGroup()
for (index, exercise) in exercises.enumerated() {
let containerView = UIView(frame: CGRect(x: xPosition, y: yPosition, width: rectangleWidth, height: rectangleWidth))
containerView.clipsToBounds = true
containerView.layer.cornerRadius = 20
containerView.layer.borderWidth = 2
containerView.layer.borderColor = UIColor(named: "Green")?.cgColor
scrollView.addSubview(containerView)
let webView = WKWebView(frame: CGRect(x: -20, y: 0, width: rectangleWidth * 1.2, height: rectangleWidth * 2))
webView.contentMode = .scaleAspectFit
containerView.addSubview(webView)
containerView.tag = index
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(exerciseTapped(_:)))
containerView.addGestureRecognizer(tapGesture)
if let url = URL(string: exercise.url) {
let request = URLRequest(url: url)
group.enter()
webView.load(request)
} else {
print("Invalid URL: \(exercise.url)")
}
if index % 2 == 0 {
xPosition += rectangleWidth + spacing
} else {
yPosition += rectangleWidth + spacing
xPosition = spacing
}
}
group.notify(queue: .main) {
print("All gifs have finished loading")
}
scrollView.contentSize = CGSize(width: screenWidth, height: yPosition + rectangleWidth - spacing - 100)
}
private func showExerciseDetails(_ exercise: ExerciseBody) {
let modalViewController = BodyDetailViewController()
modalViewController.exercise = exercise
modalViewController.modalPresentationStyle = .overFullScreen
present(modalViewController, animated: true, completion: nil)
}
@objc private func exerciseTapped(_ gesture: UITapGestureRecognizer) {
guard let index = gesture.view?.tag,
index < exercises.count else {
return
}
let exercise = exercises[index]
showExerciseDetails(exercise)
}
}
BodyDetailView控制器
import UIKit
class BodyDetailViewController: UIViewController {
var exercise: ExerciseBody?
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
}
}
答: 暂无答案
评论
collectionview
tableview