gpt4 book ai didi

ios - 为某些元素设置动画时 UIView 会排除其他元素

转载 作者:行者123 更新时间:2023-11-28 13:21:45 25 4
gpt4 key购买 nike

在我的代码中,当键盘弹出时,我已将 UITextfield 正确地移动到 View 中。但是,我一直在努力创建另一个仅作用于 UIImageView 的动画,因为当前的解决方案永久地偏移了 UIImageView 中的 Logo 。

var logoImage:UIImageView!
var logoImageX:CGFloat = 85
var logoImageY:CGFloat = 35

// Logo Code

let logo:UIImage = UIImage(named:"color-logo.png")
let logoHeight:CGFloat = 150
let logoWidth:CGFloat = logoHeight

logoImage = UIImageView(image:Logo)
logoImage.frame = CGRectMake(logoImageX,logoImageY,logoWidth, logoHeight)
self.view.insertSubview(logoImage, atIndex: 1)

// Keyboard Auto Scroll

func textFieldDidBeginEditing(textField: UITextField!) {
self.animateTextField(textField, up: true)
self.animateWithDuration(true)
}

func textFieldDidEndEditing(textField: UITextField!) {
self.animateTextField(textField, up: false)
self.animateWithDuration(false)
}

func animateTextField(textField:UITextField,up: Bool){

let movementDistance:Int = -100
let movementDuration:NSTimeInterval = 0.25
var movement = CGFloat(Int((up ? movementDistance : -movementDistance)))

UIView.beginAnimations("animateTextField", context:nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration)
self.view.frame = CGRectOffset(self.view.frame, 0, movement)
UIView.commitAnimations()

}

这是 UIImageView 的函数:

//Logo (UIImageView) Scroll
func animateWithDuration(up:Bool){
let logoMovementDuration = 0.25
let logoMovementDistance:Int = 20
var logoMovement = CGFloat(Int((up ? logoMovementDistance : -logoMovementDistance + (logoImageY))))
UIView.beginAnimations("animateWithDuration", context:nil)
UIView.setAnimationDuration(logoMovementDuration)
logoImage.frame = CGRectMake(logoImageX,logoMovement,logoImage.frame.size.width,logoImage.frame.size.height)
UIView.commitAnimations()
}

最佳答案

我已经通过创建多个 UIView 找到了解决方案,或者在我的例子中特别是 3。

代码如下所示,根据您想要实现的目标,分为不同的组以便为每个组应用动画(这些在 viewDidLoad() 之外声明以使其可公开访问):

var otherView = UIView(frame: UIScreen.mainScreen().bounds)
var logoImageView = UIView(frame:CGRect(x: 0, y: 0, width: xx, height: xx))
var resultView = UIView(frame:CGRect(x: 0, y: 0, width: xx, height: xx))

之后,我将 subview 添加到 UIView 实例,最后添加到 super View (在 viewDidLoad() 中声明):

self.logoImageView.insertSubview(logoImage, atIndex:1)
self.view.insertSubview(logoImageView, atIndex: 1)

然后最终按照 rdelmar 的建议,使用基于 block 的动画应用相同的功能。自 iOS 4.0 以来由 Apple 推荐(在 viewDidLoad() 之外和之后):

//键盘自动滚动

func textFieldDidBeginEditing(textField: UITextField!) {
self.animateTextField(textField, up: true)
}

func textFieldDidEndEditing(textField: UITextField!) {
self.animateTextField(textField, up: false)
}

func animateTextField(textField:UITextField,up: Bool){

let resultMovementDistance:Int = -xx
let movementDistance:Int = -xx
let movementDuration:NSTimeInterval = 0.xx
var resultMovement = CGFloat(Int((up ? resultMovementDistance : -resultMovementDistance)))
var otherMovement = CGFloat(Int((up ? movementDistance : -movementDistance)))
var logoMovement = otherMovement/5
UIView.animateWithDuration(
movementDuration,
delay: 0,
options: UIViewAnimationOptions.CurveEaseIn, animations:{
self.otherView.frame = CGRectOffset(self.otherView.frame,0,otherMovement)},
completion:nil)
UIView.animateWithDuration(
movementDuration,
delay: 0,
options: UIViewAnimationOptions.CurveEaseIn, animations:{
self.logoImageView.frame = CGRectOffset(self.logoImageView.frame,0,logoMovement)},
completion:nil)
UIView.animateWithDuration(
movementDuration,
delay: 0,
options: UIViewAnimationOptions.CurveEaseIn, animations:{
self.resultView.frame = CGRectOffset(self.resultView.frame,0,resultMovement)},
completion:nil)
}

注意:我确实在 block 中嵌套动画时遇到了困难,但我很高兴它成功了。我希望这有帮助。

关于ios - 为某些元素设置动画时 UIView 会排除其他元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24922888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com