Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为UIView/Image设置alpha_Ios_Swift_Uiimageview_Uiimage - Fatal编程技术网

Ios 为UIView/Image设置alpha

Ios 为UIView/Image设置alpha,ios,swift,uiimageview,uiimage,Ios,Swift,Uiimageview,Uiimage,我有一个带有标签数组的表视图和一个带有UIImage数组的UIView var menuItems = ["News", "Programm"] var currentItem = "News" var menuImages: [UIImage] = [UIImage(named: "news.png")!, UIImage(named: "programm_100.png")!] 标签的文本及其颜色由以下设置: cell.titleLabel.text = menuItems[indexP

我有一个带有标签数组的表视图和一个带有UIImage数组的UIView

var menuItems = ["News", "Programm"]
var currentItem = "News"

var menuImages: [UIImage] = [UIImage(named: "news.png")!, UIImage(named: "programm_100.png")!]
标签的文本及其颜色由以下设置:

cell.titleLabel.text = menuItems[indexPath.row]
cell.titleLabel.textColor = (menuItems[indexPath.row] == currentItem) ? UIColor.whiteColor() : UIColor.grayColor()
现在我希望图像也显示一点灰色。 我尝试将其设置为
cell.titlePicture.alpha

如何根据当前项目设置UIView的alpha密度


假设
标题图片
是一个
UIImageView
,您可以直接编辑它的alpha:

cell.titlePicture.alpha = (menuItems[indexPath.row] == currentItem) ? 0.6 : 1.0
根据您的喜好调整alpha值。示例输出:

α=1.0:

α=0.6:


假设
标题图片
是一个
UIImageView
,您可以直接编辑它的alpha:

cell.titlePicture.alpha = (menuItems[indexPath.row] == currentItem) ? 0.6 : 1.0
根据您的喜好调整alpha值。示例输出:

α=1.0:

α=0.6:


您可以使用扩展名。只需创建一个新的Swift文件并执行以下操作:

import UIKit

extension UIImage{


    func alpha(value:CGFloat)->UIImage
    {
        UIGraphicsBeginImageContextWithOptions(self.size, false, 0.0)

        let ctx = UIGraphicsGetCurrentContext();
        let area = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height);

        CGContextScaleCTM(ctx, 1, -1);
        CGContextTranslateCTM(ctx, 0, -area.size.height);
        CGContextSetBlendMode(ctx, CGBlendMode.Normal);
        CGContextSetAlpha(ctx, value);
        CGContextDrawImage(ctx, area, self.CGImage);

        let newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        return newImage;
    }
}
然后在设置图像时应用它:

UIImage(named: "news.png").alpha(0.5)

您可以使用扩展名。只需创建一个新的Swift文件并执行以下操作:

import UIKit

extension UIImage{


    func alpha(value:CGFloat)->UIImage
    {
        UIGraphicsBeginImageContextWithOptions(self.size, false, 0.0)

        let ctx = UIGraphicsGetCurrentContext();
        let area = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height);

        CGContextScaleCTM(ctx, 1, -1);
        CGContextTranslateCTM(ctx, 0, -area.size.height);
        CGContextSetBlendMode(ctx, CGBlendMode.Normal);
        CGContextSetAlpha(ctx, value);
        CGContextDrawImage(ctx, area, self.CGImage);

        let newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        return newImage;
    }
}
然后在设置图像时应用它:

UIImage(named: "news.png").alpha(0.5)

当然可以。因为有点耽搁,我只好等了。我想大概15分钟,你才能接受。但很明显你已经知道了,当然知道。因为有点耽搁,我只好等了。我想大概15分钟,你才能接受。但很明显你已经知道了。