接著上一個動畫繼續(xù)學習UIDynamic Animation 相關猿涨,通過 UIGravityBehavior 重力行為、UICollisionBehavior 碰撞行為姆怪、UIAttachmentBehavior吸附行為實現(xiàn)橡皮筋的功能叛赚。
將其創(chuàng)建為 View,通過子 View 逐一設置來特征
- RubberBaseView -- 初始化 View
- RubberAttachView -- 增加吸附行為
- RubberView -- 增加彈性行為
#import <UIKit/UIKit.h>
@interface RubberBaseView : UIView
@property (nonatomic, strong) UIImageView *box;
@property (nonatomic, strong) UIDynamicAnimator *animator;
@end
#import "RubberBaseView.h"
@implementation RubberBaseView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// 設置背景顏色(通過圖片)
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"back"]];
// 設置方塊 橡皮
_box = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rubber"]];
_box.center = self.center;
[self addSubview: _box];
// 創(chuàng)建UIDynamicAnimator
_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self];
}
return self;
}
#import "RubberBaseView.h"
@interface RubberAttachView : RubberBaseView
@property (strong, nonatomic) UIAttachmentBehavior *attachment;
@end
#import "RubberAttachView.h"
@implementation RubberAttachView {
UIImageView *_imageView;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// 添加拖動手勢
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self addGestureRecognizer:pan];
// 添加附著行為
UIOffset offset = UIOffsetMake(-25, -25);
_attachment = [[UIAttachmentBehavior alloc] initWithItem:self.box offsetFromCenter:offset attachedToAnchor:CGPointMake(self.box.center.x, self.box.center.y - 100)];
[self.animator addBehavior:_attachment];
// 添加box內(nèi)部錨點圖像
_imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"lineCircle"]];
_imageView.center = CGPointMake(self.box.bounds.size.width / 2 + offset.horizontal, self.box.bounds.size.height / 2 + offset.vertical);
[self.box addSubview:_imageView];
}
return self;
}
- (void)pan:(UIPanGestureRecognizer *)gesture {
if (UIGestureRecognizerStateChanged == gesture.state) {
self.attachment.anchorPoint = [gesture locationInView:self];
// 重新繪制
[self setNeedsDisplay];
}
}
- (void)drawRect:(CGRect)rect
{
// 上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 位置
CGPoint position = [self convertPoint:_imageView.center fromView:self.box];
// 設置開始點
CGContextMoveToPoint(context, position.x, position.y);
CGContextAddLineToPoint(context, self.attachment.anchorPoint.x, self.attachment.anchorPoint.y);
// 設置線寬
CGContextSetLineWidth(context, 5.0f);
CGFloat length[] = {5.0, 5.0};
CGContextSetLineDash(context, 0.0, length, 2);
[[UIColor blackColor] set];
// 設置路徑
CGContextDrawPath(context, kCGPathStroke);
}
@end
#import "RubberAttachView.h"
@interface RubberView : RubberAttachView
@end
#import "RubberView.h"
static NSString *const RubberViewCenterKVOName = @"center";
@implementation RubberView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// 設置物理屬性
self.attachment.damping = 1; // 震蕩
self.attachment.frequency = 1; // 阻力
// 設置觀察者稽揭,在手指離開以后繼續(xù)畫線
[self.box addObserver:self forKeyPath:RubberViewCenterKVOName options:NSKeyValueObservingOptionNew context:nil];
//創(chuàng)建并添加重力
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.box]];
[self.attachment addChildBehavior:gravity];
//創(chuàng)建并添加碰撞行為對象
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.box]];
collision.translatesReferenceBoundsIntoBoundary = YES;
[self.attachment addChildBehavior:collision];
}
return self;
}
- (void)dealloc {
[self.box removeObserver:self forKeyPath:RubberViewCenterKVOName];
}
//畫線
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:RubberViewCenterKVOName]) {
[self setNeedsDisplay];
}
}
@end
最后實現(xiàn)
#import "ViewController.h"
#import "RubberView.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
@property (nonatomic, strong) RubberView *rubberView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.rubberView];
}
- (RubberView *)rubberView {
if (!_rubberView) {
_rubberView = [[RubberView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
}
return _rubberView;
}
@end
注意點1: 此處為什么要通過 子類的 View(繼承) 逐一去設置呢俺附?
- 方便理解,一目了解
- 不改變原來的基礎上溪掀,拓充方法事镣,擴充功能性強。
當然此處也可以不這樣寫咯揪胃。
注意點2: UIAttachmentBehavior
附著行為璃哟,讓物體附著在某個點或另外一個物體上≈幌可以設置附著點的到物體的距離沮稚,阻尼系數(shù)和振動頻率等。
_attachment = [[UIAttachmentBehavior alloc] initWithItem:self.box offsetFromCenter:offset attachedToAnchor:CGPointMake(self.box.center.x, self.box.center.y - 100)];
注意這個初始化中方法的offset 和 point 的設置(就是決定線和 box 位置和距離的)册舞,當然UIAttachmentBehavior 還有其他的初始化方法蕴掏,可以根據(jù)不同場景相應使用。
- (instancetype)initWithItem:(id <UIDynamicItem>)item attachedToAnchor:(CGPoint)point;
- (instancetype)initWithItem:(id <UIDynamicItem>)item offsetFromCenter:(UIOffset)offset attachedToAnchor:(CGPoint)point NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithItem:(id <UIDynamicItem>)item1 attachedToItem:(id <UIDynamicItem>)item2;
- (instancetype)initWithItem:(id <UIDynamicItem>)item1 offsetFromCenter:(UIOffset)offset1 attachedToItem:(id <UIDynamicItem>)item2 offsetFromCenter:(UIOffset)offset2 NS_DESIGNATED_INITIALIZER;
PS:場景來自:【iOS開發(fā)范例實戰(zhàn)寶典.進階篇】调鲸。