swift 3淡化图像,而不是UIImage图层

时间:2017-05-27 17:00:40

标签: ios uiimageview

我想知道是否可以在不动画UIImageView层的情况下为UIImageView内部的视图设置动画。我希望边框不要动画,并且图像要剪切到边框,但是图像要根据需要淡入和淡出。

我是这样的动画:

func animateStart() {
    UIView.animate(withDuration: 3, animations: {
        self.firstImg.image = UIImage(named:"peter.JPG")
        self.firstImg.alpha = 1
    }, completion: { finished in
        if finished {
            self.animateSecond()
        }
    })
}

设置边框如:

class RoundedImage: UIImageView {

  override func awakeFromNib() {
    self.layer.borderWidth = 5
    self.layer.borderColor = UIColor.darkGray.cgColor
    self.layer.cornerRadius = 15
    self.clipsToBounds = true
  }

2 个答案:

答案 0 :(得分:2)

不要在图像视图上创建边框。

将边框放在UIView上,然后将图像视图添加到其中。

现在,当您为图像视图设置动画并将其淡出时。它内部的视图将保留。

答案 1 :(得分:0)

最好使用A UIView(作为你的imageView的容器)并将你的imageView放在它里面。将你想要的任何动画制作成imageView(Fade in Fade out)。

你会得到imageView框架(实际上它不是,它背后是UIView,它作为你的imageView的容器)

退色前:

enter image description here

淡出后

enter image description here

查看层次结构:

enter image description here