AFViewHelper 4.1.0

AFViewHelper 4.1.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
版本上次发布2016年7月
SPM支持 SPM

Melvin Rivera 维护。



  • Melvin Rivera

AFViewHelper

Swift 2.0 的 Autolayout 和 Animation UIVIew 扩展

Sample Project Screenshot

动画

// Required
view.animate(.Shake, curve: .EaseInOutBack)
// With optionals
view.animate(.Shake, curve: .EaseInOutBack, duration: 0.8, delay: 0, force: 1, damping: 0.7,velocity: 1, fromRotation: 0, fromScale: 1.5, fromX: 0, fromY: 0)

自动布局

let box = UIView(autoLayout:true)
view.addSubview(box)
box.backgroundColor = UIColor.redColor()
box.width(100)
box.height(100)
box.center(to: view)
view.layoutIfNeeded()

链式函数

将 100 像素的视图固定到另一个视图的中央

view.width(100).height(100).center(to: view)

压缩 & 推拉

view.horizontalCompressionPriority(UILayoutPriorityDefaultHigh)
view.horizontalHuggingPriority(UILayoutPriorityDefaultLow)

NSLayoutConstraints

您可以通过使用 pin() 或 applyAttribute() 来访问 NSLayoutConstraints。

// Pinning to an item
let widthConstraint = view.pin(.Width, to: view, attribute: .Height, constant: 0, multiplier: 0.5, relation: .LessThanOrEqual)
// Applying an attribute
let widthConstraint =  view.applyAttribute(.Width, constant: 100, multiplier: 0.5, relation: .Equal)

UIView 扩展

为自动布局做准备

如果一个视图已有框架,prepForAutoLayout() 会将其从视图中移除,启用自动布局,并将其放回视图中。

view.prepForAutoLayout()

为动画做准备

如果一个视图使用自动布局但您需要使用框架进行动画,prepForAnimation() 会将其从视图中移除,禁用自动布局,并将其放回视图中。

view.prepForAnimation()

实例化

使用自动布局实例化一个新的 UIView

convenience init(autoLayout: Bool = true)

位置

原点

返回框架的原点

origin() -> CGPoint

使用自动布局或框架固定框架的顶部和左侧

origin(constant: CGPoint) -> UIView 

使用自动布局将左侧和顶部固定到另一个视图

origin(to to:AnyObject, constant: CGPoint = CGPoint(x: 0, y: 0), multiplier:CGFloat = 1) -> UIView

返回最小 x 坐标点

left() -> CGFloat 

使用自动布局或框架固定左侧

left(constant: CGFloat) -> UIView

使用自动布局将左侧固定到另一个视图

left(to to:AnyObject, attribute: NSLayoutAttribute = .Left, constant: CGFloat = 0, multiplier:CGFloat = 1) -> UIView

leading

返回leading侧值

leading() -> CGFloat

使用自动布局或框架固定leading侧

leading(constant: CGFloat) -> UIView

使用自动布局将leading侧固定到另一个视图

leading(to to:AnyObject, attribute: NSLayoutAttribute = .Leading, constant: CGFloat = 0, multiplier:CGFloat = 1) -> UIView

返回最大 x 坐标点

right() -> CGFloat

使用自动布局或框架固定右侧

right(constant: CGFloat) -> UIView 

使用自动布局将右侧固定到另一个视图

right(to to:AnyObject, attribute: NSLayoutAttribute = .Right, constant: CGFloat = 0, multiplier:CGFloat = 1) -> UIView

跟踪

返回跟踪侧值

trailing() -> CGFloat

使用自动布局或框架固定跟踪侧

trailing(constant: CGFloat) -> UIView 

使用自动布局将跟踪侧固定到另一个视图

trailing(to to:AnyObject, attribute: NSLayoutAttribute = .Trailing, constant: CGFloat = 0, multiplier:CGFloat = 1) -> UIView

顶部

返回顶部侧值

top() -> CGFloat 

使用自动布局或框架固定顶部侧

top(constant: CGFloat) -> UIView

使用自动布局将跟踪侧固定到另一个视图

top(to to:AnyObject, attribute: NSLayoutAttribute = .Top, constant: CGFloat = 0, multiplier:CGFloat = 1) -> UIView

底部

返回底部侧值

bottom() -> CGFloat

使用自动布局或框架固定底部侧

bottom(constant: CGFloat) -> UIView 

使用自动布局将底部侧固定到另一个视图

bottom(to to:AnyObject, attribute: NSLayoutAttribute = .Bottom, constant: CGFloat = 0, multiplier:CGFloat = 1) -> UIView 

中心

使用自动布局或框架将中心固定到其父视图

center(constant: CGPoint = CGPoint(x: 0, y: 0)) -> UIView

使用自动布局将中心点固定到另一个视图

center(to to:AnyObject, constant:CGSize = CGSize(width: 0, height: 0), multiplier:CGFloat = 1) -> UIView 

中心X

返回中心X坐标

centerX() -> CGFloat

使用自动布局或框架固定中心X坐标

centerX(constant: CGFloat = 0) -> UIView 

使用自动布局将中心X坐标固定到另一个视图

centerX(to to:AnyObject, attribute: NSLayoutAttribute = .CenterX, constant: CGFloat = 0, multiplier:CGFloat = 1) -> UIView

中心Y

返回中心Y坐标

centerY() -> CGFloat

使用自动布局或框架固定中心Y坐标

centerY(constant: CGFloat = 0) -> UIView

使用自动布局将中心Y坐标固定到另一个视图

centerY(to to:AnyObject, attribute: NSLayoutAttribute = .CenterY, constant: CGFloat = 0, multiplier:CGFloat = 1) -> UIView

压缩和抱紧优先级

压缩优先级

使用自动布局返回水平轴的压缩阻力优先级

horizontalCompressionPriority() -> UILayoutPriority

使用自动布局设置水平轴的压缩阻力优先级

horizontalCompressionPriority(priority: UILayoutPriority) -> UIView  

使用自动布局返回垂直轴的压缩阻力优先级

verticalCompressionPriority() -> UILayoutPriority

使用自动布局设置垂直轴的压缩阻力优先级

verticalCompressionPriority(priority: UILayoutPriority) -> UIView

抱紧优先级

使用自动布局返回水平轴的内容抱紧优先级

func horizontalHuggingPriority() -> UILayoutPriority

使用自动布局设置水平轴的内容抱紧优先级

horizontalHuggingPriority(priority: UILayoutPriority) -> UIView

使用自动布局返回垂直轴的内容抱紧优先级

verticalHuggingPriority() -> UILayoutPriority

使用自动布局设置垂直轴的内容抱紧优先级

verticalHuggingPriority(priority: UILayoutPriority) -> UIView

大小

大小

返回框架大小

size() -> CGSize

使用自动布局或框架设置框架大小

size(constant: CGSize) -> UIView

使用自动布局将大小固定到另一个视图

size(to to:AnyObject, constant: CGSize = CGSize(width: 0, height: 0), multiplier:CGFloat = 1) -> UIView

宽度

返回框架宽度

width() -> CGFloat 

使用自动布局或框架设置框架宽度

width(constant: CGFloat) -> UIView

使用自动布局将宽度固定到另一个视图

width(to to:AnyObject, attribute: NSLayoutAttribute = .Width, constant: CGFloat = 0, multiplier:CGFloat = 1) -> UIView

高度

返回框架高度

height() -> CGFloat

使用自动布局或框架设置框架高度

eight(constant: CGFloat) -> UIView

使用自动布局将高度固定到另一个视图

height(to to:AnyObject, attribute: NSLayoutAttribute = .Height, constant: CGFloat = 0, multiplier:CGFloat = 1) -> UIView

最小尺寸

使用自动布局返回最小尺寸

minSize() -> CGSize?

使用自动布局设置最小尺寸

minSize(constant:CGSize) -> UIView

使用自动布局将最小尺寸固定到另一个视图

minSize(to to:AnyObject, constant:CGSize = CGSize(width: 0, height: 0), multiplier:CGFloat = 1)

最小宽度

使用自动布局返回最小宽度

minWidth() -> CGFloat?

使用自动布局设置最小宽度

minWidth(constant:CGFloat) -> UIView

使用自动布局将最小宽度锁定到另一个视图

minWidth(to to:AnyObject, attribute: NSLayoutAttribute = .Width, constant:CGFloat = 0, multiplier:CGFloat = 1) -> UIView

最小高度

使用自动布局获取最小高度

minHeight() -> CGFloat?

使用自动布局设置最小高度

minHeight(constant:CGFloat) -> UIView 

使用自动布局将最小高度锁定到另一个视图

minHeight(to to:AnyObject, attribute: NSLayoutAttribute = .Height, constant:CGFloat = 0, multiplier:CGFloat = 1) -> UIView

最大尺寸

使用自动布局获取最大尺寸

maxSize() -> CGSize?

使用自动布局设置最大尺寸

maxSize(constant:CGSize) -> UIView

使用自动布局将最大尺寸锁定到另一个视图

maxSize(to to:AnyObject, constant:CGSize = CGSize(width: 0, height: 0), multiplier:CGFloat = 1) -> UIView

最大宽度

使用自动布局获取最大宽度

maxWidth() -> CGFloat?

使用自动布局设置最大宽度

maxWidth(constant:CGFloat) -> UIView

使用自动布局将最大宽度锁定到另一个视图

maxWidth(to to:AnyObject, attribute: NSLayoutAttribute = .Width, constant:CGFloat = 0, multiplier:CGFloat = 1) -> UIView

最大高度

使用自动布局获取最大高度

maxHeight() -> CGFloat?

使用自动布局设置最大高度

maxHeight(constant:CGFloat) -> UIView

使用自动布局将最大高度锁定到另一个视图

maxHeight(to to:AnyObject, attribute: NSLayoutAttribute = .Height, constant:CGFloat = 0, multiplier:CGFloat = 1) -> UIView

最小和最大尺寸

获取最小边的长度

smallestSideLength() -> CGFloat

获取最大边的长度

largestSideLength() -> CGFloat

自动布局状态

通过移除视图、启用translatesAutoresizingMaskIntoConstraints并重新将其添加到其superview中来为基于框架的动画准备视图

prepForAnimation()

通过移除视图、禁用translatesAutoresizingMaskIntoConstraints并重新将其添加到其superview中来为自动布局准备视图

prepForAutoLayout()

锁定和应用

将属性锁定到另一个视图

pin(pinAttribute:NSLayoutAttribute, to:AnyObject? = nil, attribute:NSLayoutAttribute, constant:CGFloat = 0, multiplier:CGFloat = 1, relation:NSLayoutRelation = .Equal) -> NSLayoutConstraint? 

将属性应用到视图

applyAttribute(attribute:NSLayoutAttribute, constant:CGFloat = 0, multiplier: CGFloat = 1, relation:NSLayoutRelation = .Equal) -> NSLayoutConstraint

移除约束

递归地移除所有附加约束

removeAttachedConstraintsRecursevely() -> UIView

递归地移除一个约束

removeConstraintRecursevely(constraint:NSLayoutConstraint) -> UIView

方向

如果布局方向是从左到右,则返回true

layoutDirectionIsLeftToRight() -> Bool

UIView效果扩展

边框

图层边框颜色

var borderColor: UIColor 

图层边框宽度

var borderWidth: CGFloat

使用虚线图案设置图层边框

borderWithDashPattern(lineDashPattern: [Int], borderWidth: CGFloat, borderColor: UIColor, cornerRadius: CGFloat?) -> UIView 

圆角

图层圆角半径

var cornerRadius: CGFloat 

通过将角落圆滑到宽度的一半来创建一个圆形,设置边框颜色和宽度

roundCornersToCircle(borderColor: UIColor?, borderWidth: CGFloat?) -> UIView 

通过将角落圆滑到宽度的一半来创建一个圆形

roundCorners(cornerRadius: CGFloat, borderColor: UIColor?, borderWidth: CGFloat?) -> UIView

阴影

图层的阴影颜色

var shadowColor: UIColor 

图层的阴影偏移量

var shadowOffset:CGSize

图层的阴影不透明度

var shadowOpacity:Float

图层的阴影半径

var shadowRadius:CGFloat

设置图层的阴影,包括颜色、偏移量、半径、不透明度和遮罩。

shadow(color: UIColor = UIColor.blackColor(), offset: CGSize = CGSize(width: 0, height: 0), radius: CGFloat = 6, opacity: Float = 1, isMasked: Bool = false) -> UIView

渐变

设置渐变颜色图层

setGradient(colors: [UIColor], isHorizontal:Bool = false) -> UIView

渐变图层动画颜色

animateGradientToColors(colors: [UIColor], duration: CFTimeInterval = 3) -> UIView

设置渐变图层遮罩

setGradientMask(alphas:[CGFloat], isHorizontal:Bool = false) -> UIView

UIViewController扩展

方向

如果布局方向是从左到右,则返回true

layoutDirectionIsLeftToRight() -> Bool

如果水平大小类为紧凑,则返回true

horizontalSizeClassIsCompact() -> Bool

如果垂直大小类为紧凑,则返回true

verticalSizeClassIsCompact() -> Bool