ios - 如何从 iCarousel 中删除视图并重新加载数据
问题描述
我对 IOS 开发比较陌生,我已经在我的一个 ViewController 中实现了 iCarousel。我正在通过字符串数组传递一些数据,当我单击 carouselView 上的按钮时,它应该打开照片库。从照片库中选择图像后,我想从数组中删除该值并删除 iCarousel 卡的该实例。
我环顾四周,只在 obj-c 中找到代码,但我使用的是 Swift。
我附上了一张 iCarousel 的图片。当我单击相机按钮时,它会将我带到照片库,但是一旦我选择了照片,我希望删除轮播卡的那个实例。 视图控制器图像
这是我目前在 ViewController 中用于 iCarousel 的代码
class ChallengesViewController: UIViewController, TabItem, iCarouselDelegate, iCarouselDataSource {
var tabImage: UIImage?
var image:UIImage? = imageLiteral(resourceName: "camera")
var masterchallengeList = [String]()
var masterSubtitles = [String]()
var weeklySelect = [String]()
var weeklySubtitleSelect = [String]()
enum StorageType {
case userDefaults
case fileSystem
}
@IBOutlet weak var carouselView: iCarousel!
override func awakeFromNib() {
masterchallengeList = [“A “, “B”, “C”, “D”, “E”, F”, “G”]
masterSubtitles = [“A “, “B”, “C”, “D”, “E”, F”, “G”]
weeklySelect = ["","","","","","",""]
weeklySubtitleSelect = ["","","","","","",""]
for n in 0...(weeklySelect.count - 1){
weeklySelect[n] = masterchallengeList[n]
}
for n in 0...(weeklySubtitleSelect.count - 1){
weeklySubtitleSelect[n] = masterSubtitles[n]
}
}
@objc func didTapCameraImage() {
presentPhotoActionSheet()
}
override func viewDidLoad() {
super.viewDidLoad()
carouselView.type = .coverFlow
navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
navigationItem.titleView?.tintColor = UIColor.init(red: 3, green: 25, blue: 82, alpha: 1.0)
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.title = "Challenges"
}
func numberOfItems(in carousel: iCarousel) -> Int {
return weeklySelect.count
}
func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
let tempView = UIView(frame: CGRect(x: 0, y: 0, width: 250, height: 350))
let lightBlue = UIColor.init(red: 94, green: 122, blue: 255, alpha: 1.0)
tempView.clipsToBounds = true
tempView.translatesAutoresizingMaskIntoConstraints = true
tempView.layer.cornerRadius = 25.0
let actionLabel = UILabel(frame: CGRect(x: 20, y: 0, width: 200, height: 150))
actionLabel.numberOfLines = 0
actionLabel.font = actionLabel.font.withSize(25.0)
actionLabel.textColor = UIColor.white
actionLabel.text = weeklySelect[index]
tempView.addSubview(actionLabel)
let subtitleLabel = UILabel(frame: CGRect(x: 20, y: -30, width: 200, height: 150))
subtitleLabel.center.y = tempView.center.y
subtitleLabel.numberOfLines = 0
subtitleLabel.font = actionLabel.font.withSize(14)
subtitleLabel.textColor = UIColor.white
subtitleLabel.text = weeklySubtitleSelect[index]
tempView.addSubview(subtitleLabel)
let photoButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
photoButton.center.x = tempView.center.x
photoButton.center.y = tempView.center.y + 92.5
photoButton.setImage(image, for: .normal)
photoButton.setImage( imageLiteral(resourceName: "camera"), for: .normal)
let gesture = UITapGestureRecognizer(target: self,
action:#selector(didTapProfilePic))
photoButton.addGestureRecognizer(gesture)
tempView.addSubview(photoButton)
return tempView
}
func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {
if option == iCarouselOption.spacing{
return value * 2
}
return value
}
}
extension ChallengesViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate{
func presentPhotoActionSheet() {
let actionSheet = UIAlertController(title: "Profile Picture", message: "How would you like to select a profile picture", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
actionSheet.addAction(UIAlertAction(title: "Take Photo", style: .default, handler: { [weak self] _ in
self?.presentCamera()
}))
actionSheet.addAction(UIAlertAction(title: "Choose Photo", style: .default, handler: { [weak self] _ in
self?.presentPhotoPicker()
}))
present(actionSheet, animated: true)
}
func presentCamera() {
let vc = UIImagePickerController()
vc.sourceType = .camera
vc.delegate = self
vc.allowsEditing = true
present(vc, animated: true)
}
func presentPhotoPicker(){
let vc = UIImagePickerController()
vc.sourceType = .photoLibrary
vc.delegate = self
vc.allowsEditing = true
present(vc, animated: true)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true, completion: nil)
guard let selectedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage else{
return}
image = selectedImage
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
override func present(_ viewControllerToPresent: UIViewController,
animated flag: Bool,
completion: (() -> Void)? = nil) {
viewControllerToPresent.modalPresentationStyle = .fullScreen
super.present(viewControllerToPresent, animated: flag, completion: completion)
}
@objc func dismissAlert(){
customAlert.dismissAlert() }
}
解决方案
如果你只想隐藏它 iCarousel
carouselView.isHidden = true
但是,如果你想将其从其中删除,superView
你必须这样做
carouselView.removeFromSuperView()
这条线从中删除carouselView
它superView
所以在这种情况下它self.view
也许你不想carouselView
在删除之后使用superView
. 在这种情况下,您可以选择;
carouselView = nil
所以用
carouselView.removeFromSuperView()
carouselView = nil
weeklySelect.remove(at: selectedRowIndex)
推荐阅读
- python - 当数组未初始化时,哪种是创建数组堆栈的最佳方式?
- load-testing - 炮兵从有效载荷文件中重复同一行
- facebook - 有没有办法通过 Facebook Marketing API 禁用预算优化?
- typescript - 使用多个中间件时在 redux 存储上正确键入 dispatch
- java - 颤振中任务':geolocator_android:compileDebugJavaWithJavac'的执行失败
- reactjs - React-传单我可以从公共文件夹中导入图标,但不能从组件文件夹中导入
- c - libtermkey 在纯文本模式终端中不报告鼠标事件
- java - 尽管没有问题,但没有返回任何东西 public static int
- swift - 使用 Swift 中的新并发将同步函数转换为异步
- assembly - 有没有办法将浮点立即分配给寄存器