根据用户输入在集合视图中创建新图像
Posted
技术标签:
【中文标题】根据用户输入在集合视图中创建新图像【英文标题】:creating a new image in a collection view based off of user input 【发布时间】:2020-05-13 02:12:33 【问题描述】:现在我有一个收藏视图,显示一定数量的奖杯。
import UIKit
class TrophyViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource
let trophies = ["1", "2", "3"]
fileprivate let collectionView: UICollectionView =
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
layout.scrollDirection = .vertical
cv.translatesAutoresizingMaskIntoConstraints = false
cv.register(CutomCell.self, forCellWithReuseIdentifier: "cell")
return cv
()
override func viewDidLoad()
super.viewDidLoad()
view.addSubview(collectionView)
collectionView.backgroundColor = .white
collectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: 40).isActive = true
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 40).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -40).isActive = true
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -40).isActive = true
collectionView.delegate = self
collectionView.dataSource = self
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
return CGSize(width: collectionView.frame.width/2.5, height: collectionView.frame.width/2)
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
return trophies.count
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CutomCell
cell.backgroundColor = .white
return cell
class CutomCell: UICollectionViewCell
fileprivate let trophy: UIImageView =
let trophy = UIImageView()
trophy.image = #imageLiteral(resourceName: "trophy")
trophy.translatesAutoresizingMaskIntoConstraints = false
trophy.contentMode = .scaleAspectFill
trophy.clipsToBounds = true
return trophy
()
override init(frame: CGRect)
super.init(frame: frame)
contentView.addSubview(trophy)
trophy.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
trophy.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
trophy.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
trophy.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
required init?(coder: NSCoder)
fatalError("init(coder:) has not been implemented")
但是,我想制作这个集合视图,以便它根据目标完成时间显示奖杯,用户将获得一个新奖杯。 这是我的进度条。
let shapeLayer = CAShapeLayer()
override func viewDidLoad()
super.viewDidLoad()
//creates the path for the progress bar animation
let trackLayer = CAShapeLayer()
let circularPath = UIBezierPath(arcCenter: CGPoint(x: 200, y: 225), radius: 100, startAngle: -CGFloat.pi/2, endAngle: 2 * CGFloat.pi, clockwise: true)
trackLayer.path = circularPath.cgPath
trackLayer.lineWidth = 10
trackLayer.strokeColor = UIColor.lightGray.cgColor
trackLayer.fillColor = UIColor.clear.cgColor
view.layer.addSublayer(trackLayer)
shapeLayer.path = circularPath.cgPath
shapeLayer.lineWidth = 10
shapeLayer.strokeColor = UIColor.green.cgColor
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeEnd = 0
shapeLayer.lineCap = CAShapeLayerLineCap.round
view.layer.addSublayer(shapeLayer)
//creates the progress bar animation and the progress bar
func progressBarAnimation()
let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
self.increment += 0.16
basicAnimation.toValue = self.increment
basicAnimation.duration = 2
basicAnimation.fillMode = CAMediaTimingFillMode.forwards
basicAnimation.isRemovedOnCompletion = false
shapeLayer.add(basicAnimation, forKey: "basic")
self.countingIncrement += 20.0
//increments the counting label
countingLabel.count(from: 0, to: self.countingIncrement, withDuration: 2, andAnimation: .linear, andCounterType: .intType)
当用户完成目标以在集合视图中获得新奖杯时,我可以创建一个依赖于 if 语句的函数吗?如果是这样,我将如何创建此功能?还是有其他方法可以解决这个问题?
【问题讨论】:
【参考方案1】:class Trophy
var isEarned = false
var id = "a id"
class TrophyViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource
let collectionView: UICollectionView
let allTrophies = [Trophy]()//init it with all your trophies
var earnedTrophies: [Trophy]
return allTrophies.filter $0.isEarned == true
func userEarnTrophy(_ trophy: Trophy)
allTrophies.forEach
if $0.id == trophy.id
$0.isEarned = true
collectionView.reloadData()
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
//or return allTrophies.count if you want to display a disable state for unearned ones
return earnedTrophies.count
【讨论】:
以上是关于根据用户输入在集合视图中创建新图像的主要内容,如果未能解决你的问题,请参考以下文章