- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
当然,为背景设置纯色是微不足道的:
如今,流行的做法是使用“模糊”或“多云”背景作为应用程序的设计特征,而不是使用“纯灰色”。
例如,这里有几个“模糊”的背景 - 它只是一种纯色,可能有一些噪点,也可能是模糊的。
您可以到处看到类似这样的背景,考虑流行的提要应用程序(whassapp 等)。这是我们这个时代的“时尚”。
我突然想到,如果你能在 Swift 中用代码做到这一点,那就太棒了
注意:从 PNG 开始并不是一个优雅的解决方案:
希望可以从头开始以编程方式生成所有内容。
如果 Inspector 有一个 IBDesignable 风格的 slider ,那就太好了,“添加流行的‘颗粒状’背景……” - 在新时代应该是可能的!
最佳答案
根据我很久以前写的东西,这会让你开始:
@IBInspectable
属性:
noiseColor
:噪声/颗粒颜色,应用于 View 的 backgroundColor
noiseMinAlpha
:随机化噪声的最小 alphanoiseMaxAlpha
:随机化噪声的最大 alphanoisePasses
:应用噪声的次数,更多的 pass 会变慢,但可以产生更好的噪声效果noiseSpacing
:随机噪声出现的频率,间隔越大噪声越少解释:
当任何可设计的噪音属性发生变化时, View 将被标记为重绘。在绘图函数中生成 UIImage
(或者从 NSCache
中提取,如果可用的话)。
在生成方法中,每个像素都被迭代,如果像素应该是噪声(取决于间距参数),噪声颜色将应用随机 alpha channel 。这样做的次数与传递次数一样多。
.
// NoiseView.swift
import UIKit
let noiseImageCache = NSCache()
@IBDesignable class NoiseView: UIView {
let noiseImageSize = CGSizeMake(128, 128)
@IBInspectable var noiseColor: UIColor = UIColor.blackColor() {
didSet { setNeedsDisplay() }
}
@IBInspectable var noiseMinAlpha: CGFloat = 0 {
didSet { setNeedsDisplay() }
}
@IBInspectable var noiseMaxAlpha: CGFloat = 1 {
didSet { setNeedsDisplay() }
}
@IBInspectable var noisePasses: Int = 1 {
didSet {
noisePasses = max(0, noisePasses)
setNeedsDisplay()
}
}
@IBInspectable var noiseSpacing: Int = 1 {
didSet {
noiseSpacing = max(1, noiseSpacing)
setNeedsDisplay()
}
}
override func drawRect(rect: CGRect) {
super.drawRect(rect)
UIColor(patternImage: currentUIImage()).set()
UIRectFillUsingBlendMode(bounds, .Normal)
}
private func currentUIImage() -> UIImage {
// Key based on all parameters
let cacheKey = "\(noiseImageSize),\(noiseColor),\(noiseMinAlpha),\(noiseMaxAlpha),\(noisePasses)"
var image = noiseImageCache.objectForKey(cacheKey) as! UIImage!
if image == nil {
image = generatedUIImage()
#if !TARGET_INTERFACE_BUILDER
noiseImageCache.setObject(image, forKey: cacheKey)
#endif
}
return image
}
private func generatedUIImage() -> UIImage {
UIGraphicsBeginImageContextWithOptions(noiseImageSize, false, 0)
let accuracy: CGFloat = 1000.0
for _ in 0..<noisePasses {
for y in 0..<Int(noiseImageSize.height) {
for x in 0..<Int(noiseImageSize.width) {
if random() % noiseSpacing == 0 {
let alpha = (CGFloat(random() % Int((noiseMaxAlpha - noiseMinAlpha) * accuracy)) / accuracy) + noiseMinAlpha
noiseColor.colorWithAlphaComponent(alpha).set()
UIRectFill(CGRectMake(CGFloat(x), CGFloat(y), 1, 1))
}
}
}
}
let image = UIGraphicsGetImageFromCurrentImageContext() as UIImage
UIGraphicsEndImageContext()
return image
}
}
关于ios - UIView 的程序化 "fuzzy"样式背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32010777/
我是一名长期开发人员,但对 DNS 的经验并不丰富。这是我的问题: 我们的应用程序在 Amazon EC2 上为客户启动服务器。一个客户希望为每台启动的服务器使用自定义 DNS,而不是 AWS 提供的
我正在尝试以编程方式添加几个从 TextViews 扩展到 RelativeLayout 的 Tiles。 我的代码如下。 RelativeLayout.LayoutParams params = n
如果我在黑莓 (5.0.0) 上的电话应用程序中输入电话号码,我可以点击菜单按钮,其中一个选项是“添加到联系人”,这将显示添加联系人屏幕,电话号码已经人口稠密。我想在我的应用程序中做同样的事情 - 调
我试图将 Android XML 的外观与以编程方式创建它的方式关联起来,但我惨遭失败。我读过几个引用资料,指出 View 的 LayoutParams 类型必须等于父级的类型,但我只是不理解它。 给
我正在尝试创建 Joda 2.2 DateTimeFormatter 来重构我的代码。我试图重现的行为是 private String getSemesterLabel() { StringB
我几乎所有的用户界面都是以编程方式完成的,并在 Interface Builder 中进行了轻微的修改。但是 99% 的 UI 完全是用代码完成的,因为我觉得通过以下方式可以获得一定程度的灵 acti
我正在尝试学习如何以编程方式创建不同的 UI 元素。我的 UITableView 面临以下问题.. 我有 2 个 .swift 文件,一方面,我们有.. struct Settings
我想使用ONLY编程语法创建页面布局,但我无法将带有左、中、右内容 Pane 的边框容器插入到主 Pane 的顶部 Pane 中布局(底部布局也是如此)。 每次我尝试使用内容 Pane 作为父级时,都
我需要一些指示从哪里开始: 从任何使用 ALSA 播放音频的应用程序到连接的扬声器,我想获取样本并进行一些音频处理。我无法控制播放器,我希望能够处理来自任何来源的音频。基本上它将是一个 UV 计,也许
我想以编程方式(因为文件路径是在运行时生成的)为 SQL 语句创建一个单独的日志文件。我的代码看起来像这样 public static void InitializeLogger(FileInfo
如何以编程方式关闭嵌入式 jetty 服务器? 我这样启动 jetty 服务器: Server server = new Server(8090); ... server.start(); serve
我有一个派生自 QGraphicsView 的自定义类,它实现了一个插槽调用 scrollHorizontal(int dx),里面的代码很简单 void CustomView::scrollHo
我正在尝试了解在 ios 中布局 UI 的最佳实践。具体来说,我试图了解以下优点/缺点:纯编程 UI、 Storyboard和 XIB,以及 ios 6.0+ 中的自动布局。 我已经习惯了纯程序化 U
我正在尝试做类似于提出此问题的用户正在尝试做的事情: HTTPS Request From a Credential Provider DLL 关于用户个人资料的组成部分,我找到了this . Win
所以我试图为一些布局约束设置动画,但运气很差,我什么也没看到,然后我收到一条错误消息,说明它如何不能同时满足所有约束等。 我正在使用一个名为 PullableView 的类谁的动画用的是旧风格[UIV
我有大量的 Python 代码,需要对其施加符合 pep8 的样式/格式。 pep8 有各种规则的变体(如 this one by a coworker ),但最难的是处理分割太长的行和格式化修改后的
我试图让 ZeroClipboard API 工作,但看起来 setText() 函数没有效果。 提供简单示例 > here Copy to Clipboard ZeroClipboa
假设我有一个这样的错误应用程序: using System; namespace ConsoleApplication1 { class Program { stati
我是 Android 开发的新手,但遇到以下问题。我正在尝试以编程方式创建多个 ImageView 并使用 Picasso 加载图像,但我无意中发现了以下问题。 编辑:我想要实现的是我希望使用 wid
我需要以编程方式将 RDP 连接到虚拟机 (XP SP3/.NET3.5/VS 2008),(凭据已保存在 .rdp 文件中)并进行 UI 自动化测试。由于我们的域安全性,我需要以编程方式对交互式登录
我是一名优秀的程序员,十分优秀!