iOS-如何在Swift中创建没有第三方框架的自定义动画横幅

问题描述

我需要创建一个自定义的下拉横幅,该横幅将从顶部下降,然后在几秒钟内上升。 对于自定义横幅UIView,我要指的是这个问题,这是一个完美的示例,这就是我想要的,因为我的横幅也是动态的,这将根据文本调整高度。

Swift - How to set custom view by text length

,但不确定如何制作动画。我已经为动画编写了代码,并且可以完美地工作,但是不确定该动画如何与我上面提到的自定义视图一起工作。

@IBAction func buttonClicked(_ sender: Any) {
    bannerView()
}

func bannerView() {
    let container = UIView()
    container.backgroundColor = UIColor.black
    container.frame = CGRect(x: 0,y: -200,width: self.view.frame.width,height: 200)
    UIApplication.shared.windows[0].addSubview(container)
    dispatchQueue.main.async {
        UIView.animate(withDuration: 0.4,delay: 0.0,options: .curveLinear,animations: {
            container.frame = CGRect(x:0,y: 0,width: self.view.frame.size.width,height: 200)
            
        }) { (finished) in
            UIView.animate(withDuration: 0.4,delay: 2.0,animations: {
                container.frame = CGRect(x:0,height: 200)
            })
        }
    }
}

解决方法

我不喜欢在窗口中添加子视图,而不是在视图层次结构中添加风扇...但是,如果这确实是您想要做的,请尝试一下(查看代码中的注释)。

void onSerializedPropertyChange() {
    transform.localScale = new Vector3(_size,_size,_size);
}
,
@IBAction func buttonClicked(_ sender: Any) {
   var stringText = "Lorem ipsum dolor sit amet,"
    bannerNotification(text: stringText)
}


func bannerNotification(text: String) {
    let container = UIView()
    let image = UIImageView()
    let label = UILabel()
    container.frame = CGRect(x: 0,y:-100,width: self.view.frame.size.width,height: 100)
    container.backgroundColor = .blue
    image.frame = CGRect(x: 15,y: 60,width: 30,height: 30)
    image.image = UIImage(named: "passport")
    label.frame = CGRect(x: image.bounds.maxX + 35,y: 40,width: container.frame.size.width - 100,height: 50)
    label.numberOfLines = 0
    label.font = UIFont(name:"Helvetica Neue",size: 15)
    label.text = text
    container.addSubview(image)
    container.addSubview(label)
    UIApplication.shared.windows[0].addSubview(container)
    DispatchQueue.main.async {
        UIView.animate(withDuration: 0.4,delay: 0.0,options: .curveLinear,animations: {
            container.frame = CGRect(x:0,y: 0,height: 100)
            
        }) { (finished) in
            UIView.animate(withDuration: 0.4,delay: 2.0,animations: {
                container.frame = CGRect(x:0,y: -100,height: 100)
            })
        }
    }
}

尝试一下,希望对您有用。