简体   繁体   English

对角动画UIImageView

[英]Animate UIImageView Diagonally

I'd like to be able to animate my UIImageView background in a diagonal fashion. 我希望能够以对角线方式为UIImageView背景设置动画。 Meaning the background would animate and scroll from the top left of the screen towards the bottom right of the screen. 这意味着背景会从屏幕的左上角朝屏幕右下角动画并滚动。 Ideally, this would be a continuous animation. 理想情况下,这将是连续动画。

I have the directions figured out, however, what I have seems to take a copy of the background & animate it over a static background. 我已经弄清楚了指示,但是,我似乎要复制背景并在静态背景上设置动画。 This causes for a weird effect. 这导致了怪异的效果。

See gif: 看到gif: 在此处输入图片说明

Here is the code: 这是代码:

var imageView = UIImageView()


override func viewDidLoad() {
    super.viewDidLoad()

    self.setup();


}

func setup() {


    self.imageView.frame = self.view.bounds
    self.imageView.image = UIImage(named: "myimage.jpg")
    self.view.addSubview(self.imageView)

    self.scroll()


}

func scroll() {

    var theImage = UIImage(named: "myimage.jpg")!
    var pattern = UIColor(patternImage: theImage)
    var layer = CALayer()

    layer.backgroundColor = pattern.CGColor

    layer.transform = CATransform3DMakeScale(1,-1,1)
    layer.anchorPoint = CGPointMake(0, 1)

    var viewSize = self.imageView.bounds.size
    layer.frame = CGRectMake(0,0, theImage.size.width + viewSize.width, viewSize.height)

    self.imageView.layer.addSublayer(layer)

    var startPoint = CGPointZero
    var endPoint = CGPointMake(theImage.size.width, theImage.size.height + 15)

    var animation = CABasicAnimation(keyPath: "position")
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
    animation.fromValue = NSValue(CGPoint:startPoint)
    animation.toValue = NSValue(CGPoint:endPoint)
    animation.repeatCount = Float.infinity
    animation.duration = 1.0


    layer.addAnimation(animation, forKey: "position")

}

Is anyone able to figure out how to make the whole background scroll? 有谁能弄清楚如何使整个背景滚动? Do i just need a larger image, and scroll that? 我是否只需要放大图像并滚动? If that is the case, what does the contentMode of the imageView need to be set at? 在这种情况下,需要将imageView的contentMode设置为什么?

Thanks 谢谢

---- Update ----更新

I've reworked the code a bit, and have a working version of what I wanted. 我对代码进行了一些重新设计,并获得了所需的工作版本。 I'm curious for some feedback, as I'm still not 100% comfortable with animations in general (but i'm learning! :) ) 我很好奇一些反馈,因为我总体上还是不太满意动画(但我正在学习!:))

-- I removed the whole background image in general, as DuncanC suggested it wasn't serving a good purpose. -我总体上删除了整个背景图像,因为DuncanC认为这没有用。 Now, i'm just making an image layer, trying to make if sufficiently big, and looping the animation. 现在,我只是制作一个图像层,尝试使其足够大,然后循环播放动画。 Thoughts? 有什么想法吗?

override func viewDidLoad() {
    super.viewDidLoad()

    self.scroll()
}

func scroll() {

    var theImage = UIImage(named: "myimage.jpg")!
    var pattern = UIColor(patternImage: theImage)
    var layer = CALayer()
    layer.backgroundColor = pattern.CGColor

    layer.transform = CATransform3DMakeScale(1,-1,1)
    layer.anchorPoint = CGPointMake(0, 1)

    // i'm making the view large here so the animation can keep running smoothly without
    // showing what is behind our image
    // is there a better way to do this?
    var viewSize = CGSizeMake(self.view.bounds.size.height * 10, self.view.bounds.size.width * 10)
    layer.frame = CGRectMake(0,0, theImage.size.width + viewSize.width, viewSize.height)

    self.view.layer.addSublayer(layer)
    var startPoint = CGPointMake(-theImage.size.width, -theImage.size.height)
    var endPoint = CGPointMake(0, 0)

    var animation = CABasicAnimation(keyPath: "position")
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
    animation.fromValue = NSValue(CGPoint:startPoint)
    animation.toValue = NSValue(CGPoint:endPoint)
    animation.repeatCount = Float.infinity
    animation.duration = 3.0


    layer.addAnimation(animation, forKey: "position")        
}

**Result:* **结果:* 在此处输入图片说明

Your code doesn't make a lot of sense. 您的代码没有多大意义。 You have an image view that contains the image myimage.jpg, and then you create a CALayer that contains that image as a pattern color. 您有一个包含图像myimage.jpg的图像视图,然后创建一个包含该图像作为图案颜色的CALayer。 You add the layer to your image view and animate it's position. 您可以将图层添加到图像视图中并对其位置进行动画处理。

Just use a UIView animation and animate the center of your image view directly. 只需使用UIView动画并直接为图像视图的中心设置动画即可。 If you're using AutoLayout then you'll need to add horizontal and vertical position constraints, wire them up as IBOutlets, and then in your animation block change the constraints and call layoutIfNeeded() on the image view's superview. 如果使用的是AutoLayout,则需要添加水平和垂直位置约束,将它们连接为IBOutlets,然后在动画块中更改约束并在图像视图的超级视图上调用layoutIfNeeded()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM