如何在Swift的uiviewcontroller背景中显示自动滚动的图像?

时间:2016-05-11 11:19:21

标签: ios swift uiviewcontroller uiscrollview uiimageview

在我的swift应用程序中,我有一个带有一些按钮和标签的UIViewController。我认为在背景中有一个自动从右向左缓慢滚动(滑动)的图像会很酷。我设法放入后台UIImageView并分配了约束,以便它覆盖整个面板,但我不确定如何在其中滚动照片。

我有21:9比例的png文件 - 是否可以滚动它?

5 个答案:

答案 0 :(得分:0)

您可以为此

使用uiview动画
 UIView.animateWithDuration(2, delay: 0.4,
    options: [.Repeat, .Autoreverse, .CurveEaseInOut], animations: {
      // scroll automatically using scroll view method (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated.Keep animated NO**strong text**.you can scroll horizontally or vertically using offset.

    }, completion: nil)

您可以查看其他动画选项。我之前没有尝试动画滚动。 希望这有助于...:)

答案 1 :(得分:0)

将图像放在滚动视图中,并在NSTimer中不断更改滚动视图的内容偏移量。检查此链接: https://stackoverflow.com/a/3694455/6080920

答案 2 :(得分:0)

  1. 创建另一个UIImageView,并通过将新视图的主要锚点约束到旧视图的尾随锚点,将其约束为原始UIImageView

  2. 为原始IBOutlet的{​​{1}}锚点约束创建leading

  3. 将新imageView的图像设置为与原始图像相同。

  4. 注意: 为获得最佳效果,您应该使用无缝的可平铺图像。

    UIImageView

    致电slideImage()
    viewDidAppear(_:)

    编辑:

    可以找到示例项目here

答案 3 :(得分:0)

通过使用第三方,我们可以做到这一点

https://github.com/guowilling/SRInfiniteCarouselView

我们可以添加一个视图,我们可以通过这些代码。

    System.out.println("aaa".split(",", 0).length); // 1
    System.out.println("aaa".split("," , 0)[0]);    // aaa

答案 4 :(得分:0)

在Swift中

   func viewDidLoad(){
     Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(moveToNextImage), userInfo: nil, repeats: true)
    }

    func moveToNextImage (){
            let imgsCount:CGFloat = CGFloat(images.count)
            let pageWidth:CGFloat = self.imageScrollView.frame.width
            let maxWidth:CGFloat = pageWidth * imgsCount
            let contentOffset:CGFloat = self.imageScrollView.contentOffset.x

            var slideToX = contentOffset + pageWidth

            if  contentOffset + pageWidth == maxWidth{
                slideToX = 0
            }
            let currentPage:CGFloat = slideToX / pageWidth
            // Change the indicator
            self.pageController.currentPage = Int(currentPage)
            self.imageScrollView.scrollRectToVisible(CGRect(x:slideToX, y:0, width:pageWidth, height:self.imageScrollView.frame.height), animated: true)
        }