有一个问题动画某些元素UIView将排除其他元素

时间:2014-07-23 23:45:38

标签: ios animation uiview uiimageview swift

在我的代码中,当键盘弹出时,我已正确地将UITextfield移动到视图中。但是,我一直在努力创建另一个仅在UIImageView上运行的动画,因为这个当前的解决方案永久地抵消了UIImageView中的徽标。

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()
}

1 个答案:

答案 0 :(得分:2)

我通过创建多个UIViews找到了解决方案,或者在我的案例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))

之后,我将子视图添加到UIView实例,然后最终进入超级视图(在viewDidLoad()中声明):

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

然后最终使用rdelmar建议使用基于块的动画来应用相同的func,并且自iOS 4.0以来(在viewDidLoad()之外和之后)推荐Apple:

//键盘自动滚动

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)
}

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