ios - 在 UIImageView 上添加渐变

标签 ios uiimageview swift3 uicolor cagradientlayer

我想在我的 UIImageView 上添加一个子层,但它不起作用。

  • 我有一组 10 张图像,名称从 photo0photo9 并且我显示 它的定时器为 5 秒。
  • outlet shanghaiImage 是我的背景

我想在这个马蒂的顶部添加一个渐变,例如:透明(顶部)到黑色(底部)。

感谢您的帮助:)

这是我在 Swift 3 中的代码。

这部分很好:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var shanghaiImage: UIImageView!

// beginning index
var _curentImageIndex:Int = 0
// number of images
let NUMBER_OF_IMAGES:Int = 10
// creation of the Timer
var _uiTimer:Timer?


override func viewDidLoad() {
    super.viewDidLoad()
    showPhoto(atIndex: _curentImageIndex)
}

// MARK TIMER ---------

func selectNewTimer(){
    if let existingTimer:Timer = _uiTimer{
        existingTimer.invalidate()
    }
    _uiTimer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(ViewController.showNextImage), userInfo: nil, repeats: true)
}

问题就出在这里。我不知道为什么它不起作用。

// MARK PHOTO ---------
func showPhoto(atIndex index:Int){

    let photoName:String =  "photo\(index)"
    shanghaiImage.image =  UIImage(named: photoName)

    let gradient = CAGradientLayer()
    gradient.frame = shanghaiImage.bounds
    let startColor = UIColor(colorLiteralRed: 0, green: 0, blue: 0, alpha: 1)
    let endColor = UIColor.black

    gradient.colors = [startColor, endColor]
    shanghaiImage.layer.insertSublayer(gradient, at: 0)


    _curentImageIndex  =  index
    selectNewTimer()
    }

func showNextImage() {
    var nextPhotoIndex:Int = _curentImageIndex + 1
        if nextPhotoIndex >= NUMBER_OF_IMAGES {
            nextPhotoIndex = 0
        }
    showPhoto(atIndex: nextPhotoIndex)
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

最佳答案

我建议在 UIImageView 之上放置一个带有渐变的 UIView:

@IBOutlet weak var shanghaiImage: UIImageView!

let view = UIView(frame: profileImageView.frame)

let gradient = CAGradientLayer()

gradient.frame = view.frame

gradient.colors = [UIColor.clear.cgColor, UIColor.black.cgColor]

gradient.locations = [0.0, 1.0]

view.layer.insertSublayer(gradient, at: 0)

shanghaiImage.addSubview(view)

shanghaiImage.bringSubview(toFront: view)

objective-C :

UIView *view = [[UIView alloc] initWithFrame: profileImageView.frame];

CAGradientLayer *gradient = [[CAGradientLayer alloc] init];

gradient.frame = view.frame;

gradient.colors = @[ (id)[[UIColor clearColor] CGColor], (id)[[UIColor blackColor] CGColor] ];

gradient.locations = @[@0.0, @1.0];

[view.layer insertSublayer: gradient atIndex: 0];

[shanghaiImage addSubview: view];

[shanghaiImage bringSubviewToFront: view];

关于ios - 在 UIImageView 上添加渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40405798/

相关文章:

Swift 3 - 从核心数据中获取值(value)

ios - 同时使用 Firebase 和 HockeyApp 时没有 HockeyApp 崩溃报告

ios - UIWebView 委托(delegate)不会在自定义类中触发

uitableview - 理解 SDWebImage 在 UITableView 中使用的机制

swift - 拍照传给不同的UIViewController Swift 3.0

ios - 如何正确使用 VarArgs 来本地化字符串?

ios - 在React Native中获取``PhotoEditorSDK/PhotoEditorSDK.h''文件未找到错误

ios - 使用 Alamofire 时出现 Heroku "Request Interrupted"错误

ios - 如何在 AdMob 中添加中介网络?

ios - 无法使用 PhotoKit 获得从相册请求的清晰图像