Ios &引用;前/后滑块“;UIKit中的效应
我正在制作一个游戏,在这个游戏中,玩家的宇宙飞船受到的损害是通过逐步将一个健康的宇宙飞船图像替换为一个受损的宇宙飞船图像来显示的,方式是“之前/之后”滑块。我正在想办法实现这个 我正在尝试的方法是使用受损的宇宙飞船作为背景,然后在其顶部放置另一个UIView(或UIImageView),随着受损程度的增加,逐渐限制顶视图的框架 下面是我用来逐步减少顶部图像显示的代码:Ios &引用;前/后滑块“;UIKit中的效应,ios,swift,uikit,Ios,Swift,Uikit,我正在制作一个游戏,在这个游戏中,玩家的宇宙飞船受到的损害是通过逐步将一个健康的宇宙飞船图像替换为一个受损的宇宙飞船图像来显示的,方式是“之前/之后”滑块。我正在想办法实现这个 我正在尝试的方法是使用受损的宇宙飞船作为背景,然后在其顶部放置另一个UIView(或UIImageView),随着受损程度的增加,逐渐限制顶视图的框架 下面是我用来逐步减少顶部图像显示的代码: let oldFrame = playerLayer1.frame let oldOrigin = oldFra
let oldFrame = playerLayer1.frame
let oldOrigin = oldFrame.origin
let newSize = CGSize(width: oldFrame.width - amountToReduceFrameBy, height: oldFrame.height)
let newFrame = CGRect(origin: oldOrigin, size: newSize)
playerLayer1.frame = newFrame
两个问题:
1) 这只会使我的形象向左移动,而不是逐渐模糊它。相反,我试图缩小视图的大小,使其仅部分可见,而无需移动或重新缩放图像本身。我该怎么做
2) 这是解决这个问题的正确方法吗?有没有更优雅的方法可以做到这一点?已解决:
func cropToWidth(image: UIImage, width: Double) -> UIImage {
let contextImage: UIImage = UIImage(CGImage: image.CGImage!)
let contextSize: CGSize = contextImage.size
var posX: CGFloat = 0.0
var posY: CGFloat = 0.0
// these vars represent original sizes, not specified ones
var cgwidth: CGFloat = contextSize.width
var cgheight: CGFloat = contextSize.height
// See what size is longer and create the center off of that
if contextSize.width > contextSize.height {
posX = ((contextSize.width - contextSize.height) / 2)
posY = 0
cgwidth = contextSize.height
cgheight = contextSize.height
} else {
posX = 0
posY = ((contextSize.height - contextSize.width) / 2)
cgwidth = contextSize.width
cgheight = contextSize.width
}
//let rect: CGRect = CGRectMake(posX, posY, cgwidth, cgheight)
let rect: CGRect = CGRectMake(posX, posY, CGFloat(width), cgheight)
print("rect dimensions: \(rect.width), \(rect.height)")
// Create bitmap image from context using the rect
let imageRef: CGImageRef = CGImageCreateWithImageInRect(contextImage.CGImage, rect)!
// Create a new image based on the imageRef and rotate back to the original orientation
let image: UIImage = UIImage(CGImage: imageRef, scale: image.scale, orientation: image.imageOrientation)
return image
}
玩家层1的类型是什么?如果你正在制作游戏,那么你可能应该使用SpriteKit。然而,如果你想使用UIKit,为什么不直接用另一个损坏更严重的飞船的图片替换飞船的图片呢。如果检查你的主播点,另一件事会发生。如果将一个对象设置为给定位置,并且他们尝试将另一个具有不同锚点的对象设置为相同位置,则该对象将不在相同位置。playerLayer1是CALayer,但我意识到,当需要的时候,它可能会更复杂——是否可以使用简单的UIImageView显示图像的受限部分?再看SpriteKit,这主要是一个基于文本的游戏,所以UIKit真的更有意义。这是极少数图形元素之一。我正试图用一张受损程度更高的船的图片替换另一张图片,但我正试图逐步替换,所以红色部分会随着船受损程度的增加而变大。