Attachment : 连接,附着。 处理的是两个 item 之间的关系 。或者是 item 和 point 之间的关系。
Anchor: 锚点,在 UIView 中是位于中心点。(锚点可以简单理解为一个物体的重心点)
//
// UIAttachmentBehavior.h
// UIKit
//
// Copyright (c) 2012-2015 Apple Inc. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIDynamicBehavior.h>
NS_ASSUME_NONNULL_BEGIN
// 附着行为的类型
typedef NS_ENUM(NSInteger, UIAttachmentBehaviorType) {
UIAttachmentBehaviorTypeItems, // items 之间的附着关系
UIAttachmentBehaviorTypeAnchor // items 和 point 之间的关系
} NS_ENUM_AVAILABLE_IOS(7_0);
// UIFloatRange 结构体定义,定义了最大值和最小值。
typedef struct {
CGFloat minimum;
CGFloat maximum;
} UIFloatRange;
UIKIT_EXTERN const UIFloatRange UIFloatRangeZero NS_AVAILABLE_IOS(9_0);
UIKIT_EXTERN const UIFloatRange UIFloatRangeInfinite NS_AVAILABLE_IOS(9_0);
UIKIT_EXTERN BOOL UIFloatRangeIsInfinite(UIFloatRange range) NS_AVAILABLE_IOS(9_0);
UIKIT_EXTERN BOOL UIFloatRangeIsEqualToRange(UIFloatRange range, UIFloatRange otherRange) NS_AVAILABLE_IOS(9_0);
UIKIT_STATIC_INLINE UIFloatRange UIFloatRangeMake(CGFloat minimum, CGFloat maximum) {
return (UIFloatRange){minimum, maximum};
}
// 附着类
NS_CLASS_AVAILABLE_IOS(7_0) @interface UIAttachmentBehavior : UIDynamicBehavior
// item 附着某个锚点
/*
item 和锚点之间就像一个固定的杆子进行的连接,item 可以围绕锚点进行自由的旋转。
item 和 锚点之间的 distance 是不会发生变化的。
*/
- (instancetype)initWithItem:(id <UIDynamicItem>)item attachedToAnchor:(CGPoint)point;
// 上面方法是这个方法的简化
/*
item 附着某个指定的锚点。
offset 是相对于 item center 的 offset。
item center offset 的 point 和锚点之间就像一个固定的杆子进行的连接,item 可以围绕锚点进行自由的旋转。
*/
- (instancetype)initWithItem:(id <UIDynamicItem>)item offsetFromCenter:(UIOffset)offset attachedToAnchor:(CGPoint)point NS_DESIGNATED_INITIALIZER;
// 附着某个 item
/*
两个 item 的 center 是相互的 附着。(UIView 的 center 其实就是 View 自身的锚点位置)
item1 和 item2 之间就像一个固定的杆子一样进行连接着各自的锚点,一个作用力在一个 item 上后会拖拽或者推着另一个 item,items 会自由的向四周进行旋转,但是 item1 和 item2 之间总是保持相同的距离。
*/
- (instancetype)initWithItem:(id <UIDynamicItem>)item1 attachedToItem:(id <UIDynamicItem>)item2;
// 上面方法是这个方法的简化
/*
offset1 为 相对于 item1 center 的偏移量
offset2 为 相对于 item2 center 的偏移量
*/
- (instancetype)initWithItem:(id <UIDynamicItem>)item1 offsetFromCenter:(UIOffset)offset1 attachedToItem:(id <UIDynamicItem>)item2 offsetFromCenter:(UIOffset)offset2 NS_DESIGNATED_INITIALIZER;
// 下面是一些类方法 用来创建行为对象
/*!
A sliding attachment allows for relative translation of two items along a specified axis with respect to the anchor point. A sliding
attachment prevents all relative rotation of the dynamic items.
一个滑动附件锚点允许在一个指定的轴上和另一个附件锚点进行相对的移动。 一个滑动的相对附件可以防止所有的相对的旋转。
@param item1 The first of two dynamic items connected by the attachment behavior.
第一个 item
@param item2 The second of two dynamic items connected by the attachment behavior.
第二个 item
@param point The point for which each item will be attached. The anchor point will be converted to each items local corrdinate system.
item 将要附着的锚点, 锚点将要依靠的是 item 的坐标系。
@param axis Axis of allowed relative translation between local anchor point. Must be a unit vector.
允许锚点在某个向量的方向上进行相对的移动,必须是一个单位向量。
@see attachmentRange, Represents the slidable range of the attachment with respect to the anchor point along the specified axis, this range must include 0
*/
+ (instancetype)slidingAttachmentWithItem:(id <UIDynamicItem>)item1 attachedToItem:(id <UIDynamicItem>)item2 attachmentAnchor:(CGPoint)point axisOfTranslation:(CGVector)axis NS_AVAILABLE_IOS(9_0);
/*!
A sliding attachment allows for translation of the item along a specified axis with respect to the anchor point. A sliding
attachment prevents all relative rotation of the dynamic items.
@param item1 The dynamic item connected by the attachment behavior.
@param point The point for the item will be anchored by the attachment.
@param axis Axis of allowed translation for the item. Must be a unit vector.
@see attachmentRange, Represents the slidable range of the attachment with respect to the anchor point along the specified axis, this range must include 0
*/
+ (instancetype)slidingAttachmentWithItem:(id <UIDynamicItem>)item attachmentAnchor:(CGPoint)point axisOfTranslation:(CGVector)axis NS_AVAILABLE_IOS(9_0);
/*!
A limit attachment imposes a maximum distance between two dynamic items, as if they were connected by a rope.
@param item1 The first of two dynamic items connected by the attachment behavior.
@param offset1 The point, within the dynamic item and described as an offset from its center point, for the attachment behavior.
@param item2 The second of two dynamic items connected by the attachment behavior.
@param offset2 The point, within the dynamic item and described as an offset from its center point, for the attachment behavior.
@see length
*/
+ (instancetype)limitAttachmentWithItem:(id <UIDynamicItem>)item1 offsetFromCenter:(UIOffset)offset1 attachedToItem:(id <UIDynamicItem>)item2 offsetFromCenter:(UIOffset)offset2 NS_AVAILABLE_IOS(9_0);
/*!
A fixed attachment fuses two dynamic items together at a reference point.
Fixed attachments are useful for creating complex shapes that can be broken apart later.
@param item1 The first of two dynamic items connected by the attachment behavior.
@param item2 The second of two dynamic items connected by the attachment behavior.
@param point The point for which each item will be attached. The anchor point will be converted to each items local corrdinate system.
*/
+ (instancetype)fixedAttachmentWithItem:(id <UIDynamicItem>)item1 attachedToItem:(id <UIDynamicItem>)item2 attachmentAnchor:(CGPoint)point NS_AVAILABLE_IOS(9_0);
/*!
A pin attachment allows two dynamic items to independently rotate around the anchor point as if pinned together.
You can configure how far the two objects may rotate and the resistance to rotation
@param item1 The first of two dynamic items connected by the attachment behavior.
@param item2 The second of two dynamic items connected by the attachment behavior.
@param point The point for which each item will be attached. The anchor point will be converted to each items local corrdinate system
@see frictionTorque, resistance to rotation
*/
+ (instancetype)pinAttachmentWithItem:(id <UIDynamicItem>)item1 attachedToItem:(id <UIDynamicItem>)item2 attachmentAnchor:(CGPoint)point NS_AVAILABLE_IOS(9_0);
// 获取所有的 items
@property (nonatomic, readonly, copy) NSArray<id <UIDynamicItem>> *items;
// 附着行为的类型
@property (readonly, nonatomic) UIAttachmentBehaviorType attachedBehaviorType;
// 锚点 (当没设置锚点的时候是 CGPointZero )
@property (readwrite, nonatomic) CGPoint anchorPoint;
// 两个附件 point 之间的 distance
// 在创建 行为之后可以使用这个值来修改 point 之间的 距离
@property (readwrite, nonatomic) CGFloat length;
// 阻尼系数 默认是1 ,(产生弹跳的时候的衰减值)
@property (readwrite, nonatomic) CGFloat damping; // 1: critical damping
// 震荡的频率
@property (readwrite, nonatomic) CGFloat frequency; // in Hertz
// 需要克服围绕一个锚点进行旋转的力的大小。 默认是 0.0 值越大阻力越大
@property (readwrite, nonatomic) CGFloat frictionTorque NS_AVAILABLE_IOS(9_0); // default is 0.0
// 运动的范围
@property (readwrite, nonatomic) UIFloatRange attachmentRange NS_AVAILABLE_IOS(9_0); // default is UIFloatRangeInfinite
@end
NS_ASSUME_NONNULL_END
网友评论