毛毛虫

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了毛毛虫相关的知识,希望对你有一定的参考价值。

项目描述:用一串小圆球组成的毛毛虫,可以拖动其头部,让毛毛虫跟随你的手指移动。

主要技术:使用UIDynamic来实现物理仿真效果,添加物理行为和碰撞行为,并两两添加附着行为,为毛毛虫的头部添加捕捉行为。

技术分享

#import "ViewController.h"

 

@interface ViewController ()

 

@property (nonatomic,strong)UIDynamicAnimator *animator;

 

@property (nonatomic,strong)NSMutableArray *items;//盛放所有子控件的数组

 

@property (nonatomic,weak)UIView *blueView;//毛毛虫的脑袋

 

 

@property (nonatomic,strong)UIAttachmentBehavior *attachment;

 

@end

 

@implementation ViewController

 

//懒加载

 

- (NSMutableArray *)items

{

    if (_items == nil) {

        

        _items = [NSMutableArray array];

    }

    

    return _items;

 

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    

    //1.搭建界面  添加子控件

    

    [self addSubviews];

    

    //添加仿真效果

    [self addBehavior];

    

}

 

 

//添加子控件

- (void)addSubviews

{

 

    //总数

    

    int count = 6;

    for (int i = 0; i < count; i ++) {

        

        //创建 view

        

        

        CGFloat w = 40;

        CGFloat h = w;

        UIView *item = [[UIView alloc]init];

        

        if (i != count - 1) {

            

            item.frame = CGRectMake( 50 + w * i, 200, w  , h);

            item.backgroundColor = [UIColor redColor];

            item.layer.cornerRadius = w * 0.5;

        }else{

        

            item.frame = CGRectMake(50 + w * i, 180, 80, 80);

            

            item.backgroundColor = [UIColor blueColor];

            item.layer.cornerRadius = 80 * 0.5;

            

            self.blueView = item;

 

        }

        

        [self.view addSubview:item];

        

        [self.items addObject:item];

    }

    

 

}

 

- (void)addBehavior

{

 

    //1.创建物理仿真器

    

    UIDynamicAnimator *animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];

    

    //2.创建物理仿真行为

    //2.1重力行为

    

//    NSLog(@"%@",self.view.subviews);

    

    UIGravityBehavior *gravity = [[UIGravityBehavior alloc]initWithItems:self.items];

    

    //2.2碰撞行为

    

    UICollisionBehavior *collision = [[UICollisionBehavior alloc]initWithItems:self.items];

    

    collision.translatesReferenceBoundsIntoBoundary = YES;

    

    //2.3添加附着行为

    //两两添加

    

    

    for (int i = 0; i < self.items.count - 1; i ++) {

        

        UIAttachmentBehavior *attachment = [[UIAttachmentBehavior alloc]initWithItem:self.items[i] attachedToItem:self.items[i +1]];

        

//        attachment.length = 70;

        [animator addBehavior:attachment];

 

    }

    

      //3.添加行为

 

 

    [animator addBehavior:gravity];

    [animator addBehavior:collision];

    

    self.animator = animator;

}

 

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

 

    //1.获取点击的点

    

    CGPoint point= [[touches anyObject]locationInView:self.view];

    

    //2.判断点是否在蓝色 view 之内

    

    if (CGRectContainsPoint(self.blueView.frame, point)) {

        

     //添加附着行为

        

        UIAttachmentBehavior *attachment = [[UIAttachmentBehavior alloc]initWithItem:self.blueView attachedToAnchor:point];

        

        attachment.length = 0;

        [self.animator addBehavior:attachment];

        

        self.attachment = attachment;

    }

}

 

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

 

    

    //当滑动到得点

    

    CGPoint point = [[touches anyObject] locationInView:self.view];

    

    //修改锚点

    

    self.attachment.anchorPoint = point;

}

 

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

 

   //移除附着行为

    

    [self.animator removeBehavior:self.attachment];

 

}

@end

 

以上是关于毛毛虫的主要内容,如果未能解决你的问题,请参考以下文章

[HAOI2009]毛毛虫(树形dp)

[HAOI2009]毛毛虫

[HAOI2009]毛毛虫

P3174 [HAOI2009]毛毛虫

P3174 [HAOI2009]毛毛虫

P4315 月下“毛景树”