UICollectionView imageview动画

时间:2015-10-06 22:33:45

标签: ios animation uiimageview uicollectionview

我对快速/客观C相当新,我想知道是否有人可以帮助我解决与图像视图动画相关的问题(从右向左滚动)。我目前在我的VC上有一个UIImageView,它没有任何问题。但是,我认为如果我使用UICollectionView可能是最好的。有谁可以请我这么做指向正确的方向,拜托?我还没有深入处理集合视图,所以这个概念对我来说相对较新。我目前情况的代码是:

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet weak var sponsorAnimation: UIImageView!
let images = [
    UIImage(named: "runforcure")!,
    UIImage(named: "marquee")!]

var index = 0

let animationDuration: NSTimeInterval = 1
let switchingInterval: NSTimeInterval = 3

 override func viewDidLoad() {
    super.viewDidLoad()

    sponsorAnimation.image = images[index]
    animateImageView()

}

func animateImageView() {
    CATransaction.begin()

    CATransaction.setAnimationDuration(animationDuration)
    CATransaction.setCompletionBlock {
        let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(self.switchingInterval * NSTimeInterval(NSEC_PER_SEC)))
        dispatch_after(delay, dispatch_get_main_queue()) {
            self.animateImageView()
        }
    }

    let transition = CATransition()
    transition.type = kCATransitionPush
    transition.subtype = kCATransitionFromRight
    /*
    transition.type = kCATransitionPush
    transition.subtype = kCATransitionFromRight
    */
    sponsorAnimation.layer.addAnimation(transition, forKey: kCATransition)
    sponsorAnimation.image = images[index]

    CATransaction.commit()

    index = index < images.count - 1 ? index + 1 : 0

}

我理想的情况是一次显示一张图片,并且每隔3秒就会从右向左滚动。

更新:我认为我真的很接近,但是在最后两行代码中,我不知道在哪里放置self.collectionview? 。任何帮助是极大的赞赏。再次提前感谢!

import UIKit
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet weak var collectionView: UICollectionView!

let images = [UIImage(named: "oneteam"), UIImage(named: "runforcure")]

var timer = NSTimer()



override func viewDidLoad() {
    super.viewDidLoad()

    timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: Selector("switchImage"), userInfo: nil, repeats: true)


}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.images.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell

    cell.imageView?.image = self.images[indexPath.row]


    /*I don't know where to place this line of code? If I place is outside of this function then it says unresolved identifier 'indexPath'*/
    self.collectionView?.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Top, animated: true)

    return cell

}

}

提前致谢!

2 个答案:

答案 0 :(得分:0)

您可以在视图控制器上放置一个CollectionView,并创建一个与集合视图大小相同的自定义集合视图单元格(因此,一次只能在屏幕上放置一个单元格)并包含图像视图。

这将允许用户在单元格之间使用漂亮的捕捉动画来扫描图像,并且您可以使用以下方法轻松地通过计时器上的单元格的collectionview索引进行动画处理:

ObjC:

[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES];
斯威夫特:

self.collectionView?.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Top, animated: true)

设置集合视图和自定义单元教程:http://adoptioncurve.net/archives/2012/09/a-simple-uicollectionview-tutorial/

答案 1 :(得分:0)

这里有两个不同的行动。

首先,您需要使用数据填充集合视图。这就是你在 cellForItemAtIndexPath 中所做的。

其次,您需要为刚刚填充了数据(图像)的集合视图设置动画。

如果您想要全屏,只需将每个单元格大小设置为集合视图的边界。

在viewDidLoad中,请务必拥有集合视图设置和数据就绪的数据源。

之后,您可以设置计时器以开始滚动索引路径。您需要构建自己的NSIndexPath对象。如果只有一个部分,你只需要跟踪照片数组索引(如行)。

最后,您需要一个条件来了解您何时在照片的最后停止计时器(行&lt; images.count)。