简体   繁体   English

如何使用/不使用UIBezierPath在Swift中循环图像?

[英]How do I circulate an image in Swift using/without using UIBezierPath?

I am trying to make an image circulating using UIBezierPath . 我正在尝试使用UIBezierPath使图像循环。 Here is the code I am using to determine the image's x and y coordinates, its width and its height. 这是我用来确定图像的xy坐标,其宽度和高度的代码。

var frm : CGRect = self.image.frame
    let originX = frm.origin.x
    let originY = frm.origin.y
    let originWidth = frm.size.width
    let originHeight = frm.size.height
    self.image.frame = frm

However, if I put originX and originY in this code: 但是,如果我在此代码中放入originXoriginY

let path = UIBezierPath()
    path.moveToPoint(CGPoint(x: originX+50, y: originY))
    path.addCurveToPoint(CGPoint(x: originX-20, y: originY),
    controlPoint1: CGPoint(x: originX+20, y: originY),
    controlPoint2: CGPoint(x: originX-10, y: originY))

    let anim = CAKeyframeAnimation(keyPath: "position")

    // set the animations path to our bezier curve
    anim.path = path.CGPath

    anim.repeatCount = Float.infinity
    anim.duration = 5.0

    image.layer.addAnimation(anim, forKey: "animate position along path")

The image just stays at the top left of the screen (instead of being placed close to the center). 图像仅停留在屏幕的左上方(而不是靠近中心放置)。 Why? 为什么? By the way, are there any other ways to make an image circulating other than using UIPathBezier ? 顺便说一句,除了使用UIPathBezier之外,还有其他方法可以使图像循环吗?

Well, first of all you need to override the UIBezierPath() constructor so you can give parameters as originX and originY , also you must use var instead of let because var is a simple variable and let is a constant and it cannot be modified in Swift. 好吧,首先,您需要重写UIBezierPath()构造函数,以便可以将参数指定为originXoriginY ,还必须使用var而不是let因为var是一个简单变量,let是一个constant ,并且不能在Swift中进行修改。

I dont have Xcode with me but the code should look like this: 我没有Xcode,但代码应如下所示:

override func UIBezierPath(#x:CGFloat, andOriginX:CGFloat) {
}

let path = UIBezierPath(x, y)
    path.moveToPoint(CGPoint(x: x+50, y: y)) // and so one...
    path.addCurveToPoint(CGPoint(x: originX-20, y: originY),
    controlPoint1: CGPoint(x: originX+20, y: originY),
    controlPoint2: CGPoint(x: originX-10, y: originY))

    let anim = CAKeyframeAnimation(keyPath: "position")

    // set the animations path to our bezier curve
    anim.path = path.CGPath

    anim.repeatCount = Float.infinity
    anim.duration = 5.0
    image.layer.addAnimation(anim, forKey: "animate position along path")
)

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

相关问题 如何使用UIBezierPath裁剪图像? - How to crop the image using UIBezierPath? 使用UIBezierPath:byRoundingCorners:与Swift 2和Swift 3 - Using UIBezierPath:byRoundingCorners: with Swift 2 and Swift 3 如何使用UIBezierPath使用drawInRect绘制UIImage? - How do I make a UIImage being drawn with drawInRect using a UIBezierPath? 如何通过使用UIBezierPath为Button创建特定的形状,如下面的带有Swift代码的附加图像所示? - How to create particular shape for Button by using UIBezierPath as shown in below Attached Image with Swift Code? 如何使用Swift从UIBezierPath创建虚线SKShapeNode? - How Can I Create a Dashed Line SKShapeNode from a UIBezierPath, Using Swift? 使用UIBezierPath裁剪图像-入门 - Crop an image using UIBezierPath - Beginner 使用UIBezierPath确实可以在Rubymotion中进行绘制 - Using UIBezierPath do draw in Rubymotion 如何在不使用 Storyboard 的情况下创建新的 Swift 项目? - How do I create a new Swift project without using Storyboards? 如何将图像保存在核心数据中然后进行检索? 使用迅捷 - How do i save an image in core data then retrieve it? Using swift 如何使用Swift在iOS中使用透明设置背景图片? - How do I set a background image in iOS with transparency using Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM