zoukankan      html  css  js  c++  java
  • 毛毛虫

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

    主要技术:使用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

  • 相关阅读:
    CentOS7.4部署Python3+Django+uWSGI+Nginx
    测试:ATM
    JDBC_增删改
    HTTP请求状态码
    Servlet2
    Java日期时间3
    Servlet1
    安装Tomcat
    Java日期时间2
    广度优先遍历
  • 原文地址:https://www.cnblogs.com/donghaoios/p/5196287.html
Copyright © 2011-2022 走看看