- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我为此制作了一个自定义类。但它仅适用于左上角,不适用于其他角:
@IBDesignable
public class RoundedView: UIView {
@IBInspectable public var topLeft: Bool = false
@IBInspectable public var topRight: Bool = false
@IBInspectable public var bottomLeft: Bool = false
@IBInspectable public var bottomRight: Bool = false
@IBInspectable public var cornerRadius: Int = 0
public override func awakeFromNib() {
var options = UIRectCorner()
if topLeft { options = options.union(.topLeft) }
if topRight { options = options.union(.topRight) }
if bottomLeft { options = options.union(.bottomLeft) }
if bottomRight { options = options.union(.bottomRight) }
let path = UIBezierPath(roundedRect:self.bounds,
byRoundingCorners:options,
cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
self.layer.mask = maskLayer
}
}
这是在模拟器中运行时的样子,我为所有 4 个角应用角半径:
这是怎么回事? TopLeft 起作用,TopRight 稍微起作用,底角根本不起作用。我很困惑。
最佳答案
您应该在 layoutSubviews
的覆盖中更新掩码。当约束更新 View 的 frame
时,调用 layoutSubviews
,因此这是更新掩码的正确位置。
顺便说一句,我不鼓励使用 awakeFromNib
来配置 View 。如果你使用 Storyboard,它工作正常,但以编程方式创建的 View 不会调用它。如果你确实想在创建 View 时配置 View ,最好将代码放在一些由 init(frame:)
和 init(coder:)
。这样它适用于 Storyboard和以编程方式创建的 View 。
我可能还会建议观察者检查可检查的属性,以触发圆角的更新。如果更改这些属性,您希望圆角自动更新。
因此:
@IBDesignable
public class RoundedView: UIView {
@IBInspectable public var topLeft: Bool = false { didSet { setNeedsLayout() } }
@IBInspectable public var topRight: Bool = false { didSet { setNeedsLayout() } }
@IBInspectable public var bottomLeft: Bool = false { didSet { setNeedsLayout() } }
@IBInspectable public var bottomRight: Bool = false { didSet { setNeedsLayout() } }
@IBInspectable public var cornerRadius: CGFloat = 0 { didSet { setNeedsLayout() } }
public override func layoutSubviews() {
super.layoutSubviews()
var options = UIRectCorner()
if topLeft { options.formUnion(.topLeft) }
if topRight { options.formUnion(.topRight) }
if bottomLeft { options.formUnion(.bottomLeft) }
if bottomRight { options.formUnion(.bottomRight) }
let path = UIBezierPath(roundedRect: bounds,
byRoundingCorners: options,
cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
layer.mask = maskLayer
}
}
或者,如果针对 iOS 11 及更高版本,我们可以让 CoreAnimation 进行舍入:
@IBDesignable
public class RoundedView: UIView {
@IBInspectable public var topLeft: Bool = false { didSet { updateCorners() } }
@IBInspectable public var topRight: Bool = false { didSet { updateCorners() } }
@IBInspectable public var bottomLeft: Bool = false { didSet { updateCorners() } }
@IBInspectable public var bottomRight: Bool = false { didSet { updateCorners() } }
@IBInspectable public var cornerRadius: CGFloat = 0 { didSet { updateCorners() } }
public override init(frame: CGRect = .zero) {
super.init(frame: frame)
updateCorners()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
updateCorners()
}
}
private extension RoundedView {
func updateCorners() {
var corners = CACornerMask()
if topLeft { corners.formUnion(.layerMinXMinYCorner) }
if topRight { corners.formUnion(.layerMaxXMinYCorner) }
if bottomLeft { corners.formUnion(.layerMinXMaxYCorner) }
if bottomRight { corners.formUnion(.layerMaxXMaxYCorner) }
layer.maskedCorners = corners
layer.cornerRadius = cornerRadius
}
}
后一种方法的好处在于,如果我们碰巧为 View 的 frame
设置动画,CoreAnimation 将遵守我们的圆角:
如果您使用前面提到的 layoutSubviews
方法,那么您必须手动管理动画(例如使用 CADisplayLink
)。
关于ios - 将圆角半径应用于 Storyboard 中的特定 UIView 角不适用于所有角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56383129/
我在 iOS 项目中创建了一个新目标并添加了一些我的文件以及一个新的 Storyboard文件。但是当在模拟器中运行时(我还没有尝试过该设备)它会吐在 UIApplicationMain(argc,
The document "Storyboard.storyboard" could not be opened. Could not read archive. Please use a newer
我的 .travis.yml 文件: language: objective-c script: xctool -project ProjectName. xcodeproj -scheme
我正在使用 Storyboard,因为我有一个定制的表格。我需要在单元格内的标签上显示值。我尝试为它创建一个 IBOutlet,但它似乎不接受它,它给了我一个错误消息,“连接“xyz”不能将原型(pr
Xcode 6 界面生成器默认有新的复选框“使用大小类别”。它使 View 具有适应性。 当我尝试在 Storyboard中的两个 View 之间进行连接时,我有新的选择: 代替旧的: 现在我们有“s
我在新的 xcode beta 版本中打开我的项目时犯了一个错误 现在我无法用稳定版本打开它,因为 The document “Main.storyboard” requires at least X
如果我有一个已经使用 Storyboard 的 Xcode 项目,现在我想切换到以编程方式加载 View Controller ,我该怎么做? 最佳答案 这是一个in-depth article关于这
[错误]:**构建失败** [错误]:以下构建命令失败: [错误]:编译Storyboard LaunchScreen.storyboard [错误]:(1 次失败) 如何解决这个问题?它不会在 io
我试图找到一种方法来处理多个目标(从一个项目部署多个应用程序),我不知道如何定义UIColor并在 Storyboard 中使用它(我可以为UIImage做得很好)。我的想法是使用宏根据目标切换此颜色
我有一个在 Xcode 5.1 中构建的带有 2 个 Storyboard的项目。 当我在 Xcode 6 中打开其中任何一个时,我收到此错误。他们从未在任何测试版中打开过,所以我认为这不是 Xcod
我想将 StoryBoard 添加到以编程方式制作的项目中,是否可以这样做。 最佳答案 是的,您可以向 Xcode 项目添加新的 Storyboard。我正在分享以下屏幕截图,请检查: 您可以为该 S
我将几个配置文件分成多个 Storyboard以尝试帮助我减少卡住时间,因为我听说许多 Storyboard可能是 Xcode 有时会“卡住”长达 10 分钟的原因,有时会导致我的流程崩溃工作的。所以
我是 iOS 开发新手,我知道这是一个非常简单的问题,但我真的无法理解原因。 当我构建 SingleViewApplication 时,Xcode 自动生成 LaunchScreen.storyboa
为什么 Storyboard.SetTargetName 有效而 Storyboard.SetTarget 无效?这里 xaml -
我使用的是 Xcode 7.1,我正在使用 Storyboard 引用转到不同的 Storyboard 。问题是每次我运行模拟器时,都会出现一个错误,提示 “没有找到从 Main.storyboard
因此,我开始在XCode 4.3.2中使用 Storyboard 。我从Master-Detail应用程序(iPad的拆分 View 应用程序)开始。在详细信息 View (名为DetailVC)中,
我想在我的应用程序中加载辅助 StoryboardTest.storyboard(在Main.storyboard旁边),如下所示: let storyboard = UIStoryboard(nam
我有情节提要,其中假设Controller1有按钮 当我们按下按钮时,它应该移至下一个控制器,但是我无法访问它。能告诉我如何访问第二个Storybaord,其中一个名为Profile的TabbarCo
这是我在尝试启动主 Storyboard时看到的内容,并且 launchscreen.storyboard 也遇到同样的问题。 当我尝试访问 main.storyboard 时,我看到的是 XML 代
我在 main.storyboard 的 View Controller 中有一个 label1我在 Storyboard的 View Controller 中有另一个名为 Second.storyb
我是一名优秀的程序员,十分优秀!