如何创建具有多个图像切换和删除所选图像的ImageView?

时间:2016-04-16 03:05:24

标签: ios swift image uiimageview

我有一个带有多个图像的imageView,每5秒钟切换一次。如何在我的项目中添加一个按钮,删除当前屏幕上的图片并继续在左侧图像之间切换?到目前为止,我有以下代码,谢谢!

  var uiimageArray = [UIImage]()

@IBOutlet weak var customImageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    uiimageArray += [UIImage(named: "img1")!, UIImage(named:"img2")!, UIImage(named:"img3")!, UIImage(named:"img4")!,UIImage(named:"img5")!,UIImage(named:"img6")!]

    customImageView.animationImages = uiimageArray
    customImageView.animationDuration = 5
    customImageView.startAnimating()




}

2 个答案:

答案 0 :(得分:0)

按照以下步骤

目标C

for (UIImage *image in contentArray) {

    if ([imageView.image isEqual:image]) {

        [contentArray removeObject:image];
    }
}

Swift版本:

    for image in contentArray {

        if ((customImageView.image? .isEqual(image)) != false) {

            image.removeObject(contentArray)
        }
    }

您可以检查该图像,找出该图像中当前可见的图像。

希望有所帮助......

答案 1 :(得分:0)

在按钮上单击从imageView获取currentImage并从数组中删除它。

@IBAction func btnClick(sender: UIButton) {
    var currentImage = customImageView.image
    if contains(imageArray, currentImage) { 
        imageArray.removeObjectsInArray(currentImage)
    }

}

要从数组中删除对象,您需要编写扩展方法,如下所示:

extension Array where Element: Equatable { 
    mutating func removeObject(object: Element) { 
        if let index = self.indexOf(object) { 
           self.removeAtIndex(index)
        } 
    } 

    mutating func removeObjectsInArray(array: [Element]) { 
        for object in array { 
            self.removeObject(object) 
        } 
    }
}

或者你可以看到CAKeyFrameAnimation,因为它有委托方法可以使用。