IOS7动力系统

1.

#import "ViewController.h"

@interface ViewController ()<UICollisionBehaviorDelegate>
{
    UIDynamicAnimator *_animator;//力学动画

    UIGravityBehavior *_gravity;//重力行为
    UICollisionBehavior *_collision;//碰撞行为
    //
    BOOL _firstContact;
}

@property(nonatomic,strong)UIView *squareView;
@property(nonatomic,strong)UIPushBehavior *pushBehavior;//推移行为
@property(nonatomic,strong)UISnapBehavior *snapBehavior;//吸附行为
@property(nonatomic,strong)UIDynamicAnimator *dAnimator;//力学动画

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *square = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    square.backgroundColor = [UIColor grayColor];
    [self.view addSubview:square];

    _animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];

    //添加重量行为
    _gravity = [[UIGravityBehavior alloc]initWithItems:@[square]];
    [_animator addBehavior:_gravity];

    //添加碰撞行为
    _collision = [[UICollisionBehavior alloc]initWithItems:@[square]];
    _collision.translatesReferenceBoundsIntoBoundary = YES;
    [_animator addBehavior:_collision];

    //
    UIView *barrier = [[UIView alloc]initWithFrame:CGRectMake(0, 300, 130, 20)];
    barrier.backgroundColor = [UIColor redColor];
    [self.view addSubview:barrier];

    //添加碰撞行为
//    _collision = [[UICollisionBehavior alloc]initWithItems:@[square,barrier]];
//    _collision.translatesReferenceBoundsIntoBoundary = YES;
//    [_animator addBehavior:_collision];

    //为碰撞行为添加碰撞的边界
    CGPoint rightEdge = CGPointMake(barrier.frame.origin.x+barrier.frame.size.width, barrier.frame.origin.y);
    [_collision addBoundaryWithIdentifier:@"barrier" fromPoint:barrier.frame.origin
                                  toPoint:rightEdge];
    //************************************part2**************************************
    _collision.action = ^{
//        NSLog(@"%@,%@",NSStringFromCGAffineTransform(square.transform),NSStringFromCGPoint(square.center));
    };
    //设置碰撞行为代理
    _collision.collisionDelegate = self;
    //辅助行为
    //设置它们的物理属性(如质量或弹性系数)
//    UIDynamicItemBehavior *itemBeHaviour = [[UIDynamicItemBehavior alloc]initWithItems:@[square]];
//    itemBeHaviour.elasticity = 0.6;//弹性系数属性
//    itemBeHaviour.friction = 0.6;//摩擦系数
//    itemBeHaviour.density = 9.8;//密度
//    itemBeHaviour.resistance = 0.6;//阻力
//    itemBeHaviour.angularResistance = 0.1;//转动阻力
//    itemBeHaviour.allowsRotation = NO;//允许旋转
//    [_animator addBehavior:itemBeHaviour];

}

#pragma mark -collisionDelegate Methods-
-(void)collisionBehavior:(UICollisionBehavior *)behavior
     beganContactForItem:(id<UIDynamicItem>)item
  withBoundaryIdentifier:(id<NSCopying>)identifier
                 atPoint:(CGPoint)p{
    NSLog(@"Boundary contact occurred - %@", identifier);
    UIView *view = (UIView *)item;
    view.backgroundColor = [UIColor yellowColor];
    [UIView animateWithDuration:0.3 animations:^{
        view.backgroundColor= [UIColor grayColor];
    }];
    //动态添加行为
    if (!_firstContact) {
        _firstContact = YES;
        UIView *square = [[UIView alloc]initWithFrame:CGRectMake(30, 0, 100, 100)];
        square.backgroundColor = [UIColor grayColor];
        [self.view addSubview:square];

        [_collision addItem:square];
        [_gravity addItem:square];
        //附件行为
        UIAttachmentBehavior *attach = [[UIAttachmentBehavior alloc]initWithItem:view
                                                                  attachedToItem:square];
        [_animator addBehavior:attach];
    }
}

//////////////////////////////////////////part3推移行为和吸附行为
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self createSmallSquareView];
    [self createGestureRecognizer];
    [self createAnimatorAndBehaviors];

}

- (void) createSmallSquareView{
    self.squareView =[[UIView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 80.0f, 80.0f)];

    self.squareView.backgroundColor = [UIColor greenColor];
    self.squareView.center = self.view.center;

    [self.view addSubview:self.squareView];
}

- (void) createGestureRecognizer{  //侦测视图单击
    UITapGestureRecognizer *tapGestureRecognizer =
    [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(handleTap:)];
    [self.view addGestureRecognizer:tapGestureRecognizer];
}

- (void) createAnimatorAndBehaviors{
    self.dAnimator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

    //碰撞行为
    UICollisionBehavior *collision = [[UICollisionBehavior alloc]
                                      initWithItems:@[self.squareView]];
    collision.translatesReferenceBoundsIntoBoundary = YES;
    [self.dAnimator addBehavior:collision];
    //推移行为
    self.pushBehavior = [[UIPushBehavior alloc]
                         initWithItems:@[self.squareView]
                         mode:UIPushBehaviorModeContinuous];//推移模式

    [self.dAnimator addBehavior:self.pushBehavior];

}

/*
 ///推移行为
- (void) handleTap:(UITapGestureRecognizer *)paramTap{
    CGPoint tapPoint = [paramTap locationInView:self.view];
    CGPoint squareViewCenterPoint = self.squareView.center;
    //设置推移的角度
    CGFloat deltaX = tapPoint.x - squareViewCenterPoint.x;
    CGFloat deltaY = tapPoint.y - squareViewCenterPoint.y;
    CGFloat angle = atan2(deltaY, deltaX);
    [self.pushBehavior setAngle:angle];

    //勾股
    CGFloat distanceBetweenPoints =
    sqrt(pow(tapPoint.x - squareViewCenterPoint.x, 2.0) +
         pow(tapPoint.y - squareViewCenterPoint.y, 2.0));
    //double pow(double x, double y);计算以x为底数的y次幂
    //double sqrt (double);开平方

    [self.pushBehavior setMagnitude:distanceBetweenPoints / 200.0f]; //推力的大小(移动速度)
    //每1个magnigude将会引起100/平方秒的加速度,这里分母越大,速度越小
}
*/

///吸附行为
- (void) handleTap:(UITapGestureRecognizer *)paramTap{
    CGPoint tapPoint = [paramTap locationInView:self.view];

    if (self.snapBehavior != nil){
        [self.dAnimator removeBehavior:self.snapBehavior];
    }
    self.snapBehavior = [[UISnapBehavior alloc] initWithItem:self.squareView
                                                 snapToPoint:tapPoint];
    self.snapBehavior.damping = 0.5f;  //剧列程度
    [self.dAnimator addBehavior:self.snapBehavior];
}

@end
时间: 2024-08-13 15:08:05

IOS7动力系统的相关文章

iOS7中计算UILabel中字符串的高度

iOS7中出现了新的方法计算UILabel中根据给定的Font以及str计算UILabel的frameSize的方法.本人提供category如下: UILabel+StringFrame.h ////  UILabel+StringFrame.h//  LabelHeight////  Copyright (c) 2014年 Y.X. All rights reserved.//#import <UIKit/UIKit.h>@interface UILabel (StringFrame)- 

IOS7 TableView适配

ios7下的app都是全屏的,意思就是所有控制器的view默认都是从屏幕的(0,0)开始. 为了达到全屏效果的app,官方为UIviewController增加了几个属性: 1 @property(nonatomic,assign) UIRectEdge edgesForExtendedLayout NS_AVAILABLE_IOS(7_0); // Defaults to UIRectEdgeAll 2 @property(nonatomic,assign) BOOL extendedLayo

iOS7新特性 ViewController转场切换(三) 自定义视图控制器容器的切换---非交互式

@继续前面的内容,这一章,主要介绍自定义ViewController容器上视图VC的切换.先来看看系统给我们提供的容器控制器 UINavigationController和UITabBarController 都有一个NSArray类型的属性viewControllers,很明显,存储的就是需要切换的视图VC.同理,我们定义一个ContainerViewController,是UIViewController的直接子类,用来作为容器依托,额,其他属性定义详见代码吧,这里不多说了.(PS:原先我进

【从零学习openCV】IOS7人脸识别实战

前言 接着上篇<IOS7下的人脸检測>,我们顺藤摸瓜的学习怎样在IOS7下用openCV的进行人脸识别,实际上非常easy,因为人脸检測部分已经完毕,剩下的无非调用openCV的方法对採集到的人脸样本进行训练,终于得到一个能够预測人脸的模型.可是当中的原理可谓是博大精深,因为快临最近末考试了,没时间去琢磨当中详细的细节,这次就先写个大概的demo,下次更新文章就得到6月20号之后了. 原理: 从OpenCV2.4之后,openCV增加了新的类FaceRecognizer,我们能够使用它便捷地进

iOS7上在xib中使用UITableViewController设置背景色bug

今天用xcode5.1设置xib中,用静态的方式设置UITableViewController中的tableview,把tableview中的backgroundColor改变后,xib上有效果,但是一运行就变成了透明色,在过渡动画时,都可以看到背面的view!见下面截图 后来在viewdidload中设置一下就好了 self.tableView.backgroundColor = [UIColor blackColor]; 我感觉这是xcode的一个bug! 而且这种static的设置方式,如

通讯录--(适配iOS7/8/9)

导入库#import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h> #pragma mark  点击 弹出通讯录 - (IBAction)contactClicked:(id)sender { //1. 创建联系人选择控制器 ABPeoplePickerNavigationController *picker = [ABPeoplePickerNavigationController new]

iOS7.0后隐藏状态栏(UIStatusBar)

现象: 升级到iOS7后,UIStatusBar的出现导致现有UI界面乱掉了. 原因: 由于写死了某些控件的绝对位置,原先隐藏UIStatusBar的代码没有在iOS7中起作用 解决方法: iOS7以下版本隐藏UIStatusBar的方法: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [application setSt

iOS开发- iOS7显示偏差(UITableView下移)解决的方法

之前碰到过一个问题. 就是利用storyboard拖动出来的控件, 在iOS7上跑老是莫名的下移. 比方这样(红色区域为多余的) 解决的方法: iOS7在Conttoller中新增了这个属性: automaticallyAdjustsScrollViewInsets,当设置为YES时(默认YES),假设视图里面存在唯一一个UIScrollView或其子类View.那么它会自己主动设置对应的内边距.这样能够让scroll占领整个视图,又不会让导航栏遮盖. 我们设置automaticallyAdju

iOS7中UIView的animateKeyframesWithDuration方法讲解

在iOS7中,给UIView添加了一个方法用来直接使用关键帧动画而不用借助CoreAnimation来实现,那就是animateKeyframesWithDuration 以下是使用源码: // // ViewController.m // // Created by YouXianMing on 14/11/26. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import "ViewController.h"