IOS基础之愤怒的小方块
生活随笔
收集整理的這篇文章主要介紹了
IOS基础之愤怒的小方块
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
IOS基礎之憤怒的小方塊
// // ViewController.m // 18-憤怒的小方塊 // // Created by 魯軍 on 2021/4/17. //#import "ViewController.h"typedef struct YHValue YHValue; struct YHValue {CGFloat startValue;CGFloat endValue;}; static inline YHValue YHValueMake(CGFloat startValue,CGFloat endValue){YHValue value;value.startValue = startValue;value.endValue = endValue;return value; }@interface ViewController () <UICollisionBehaviorDelegate> @property(nonatomic,strong)UIDynamicAnimator *animator; @property(nonatomic,weak)UIView *pigView; @property(nonatomic,strong)UIGravityBehavior *gravity;- (IBAction)refresh:(id)sender;@end@implementation ViewController- (IBAction)refresh:(id)sender {[self setupUI]; }- (UIDynamicAnimator *)animator{if(!_animator){_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];}return _animator; }- (void)viewDidLoad {[super viewDidLoad];[self setupUI];}-(void) setupUI{UIView *birdView = [[UIView alloc] initWithFrame:CGRectMake(150, 250, 30, 30)];birdView.backgroundColor = [UIColor redColor];[self.view addSubview:birdView];UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];[birdView addGestureRecognizer:pan];UIView *pigView = [[UIView alloc] initWithFrame:CGRectMake(500, 250, 30, 30)];pigView.backgroundColor = [UIColor blueColor];self.pigView = pigView;[self.view addSubview:self.pigView];//碰撞行為UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[pigView,birdView]];collision.translatesReferenceBoundsIntoBoundary = YES;//設置代理collision.collisionDelegate = self;[self.animator addBehavior:collision]; }-(void)pan:(UIPanGestureRecognizer *)sender{CGPoint currentPoint = [sender locationInView:self.view];CGFloat offsetX = sender.view.center.x - currentPoint.x;CGFloat offsetY = sender.view.center.y - currentPoint.y;CGFloat distance = sqrt(offsetX * offsetX + offsetY * offsetY);//繪制一個范圍 圓的范圍CGMutablePathRef path = CGPathCreateMutable();CGPathAddArc(path, NULL, sender.view.center.x, sender.view.center.y, 100, 0, 2*M_PI, 1);if(CGPathContainsPoint(path, NULL, currentPoint, NO)){if(sender.state==UIGestureRecognizerStateEnded){//添加重力UIGravityBehavior * gravity = [[UIGravityBehavior alloc] initWithItems:@[sender.view]];self.gravity = gravity;//添加推力UIPushBehavior *push = [[UIPushBehavior alloc] initWithItems:@[sender.view] mode:UIPushBehaviorModeInstantaneous];//瞬時推力push.pushDirection = CGVectorMake(offsetX, offsetY);//添加量級push.magnitude = [self resultWithConsult:distance andResultValue:YHValueMake(0, 1) andConsultValue:YHValueMake(0, 100)];[self.animator addBehavior:gravity];[self.animator addBehavior:push];/*NSLog(@"%f",push.magnitude);NSLog(@"%f",distance);*/}}else{return;}//獲取移動的偏離量CGPoint offset = [sender translationInView:sender.view];//改變紅色的 view 的 transformsender.view.transform = CGAffineTransformTranslate(sender.view.transform, offset.x, offset.y);//歸0[sender setTranslation:CGPointZero inView:self.view]; }- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item1 withItem:(id<UIDynamicItem>)item2 atPoint:(CGPoint)p{NSLog(@"6666");[self.gravity addItem:self.pigView]; }-(CGFloat)resultWithConsult:(CGFloat)consule andResultValue:(YHValue) resultValue andConsultValue:(YHValue) consultValue{//0 - 100//0 - 1CGFloat a = (resultValue.startValue - resultValue.endValue) / (consultValue.startValue - consultValue.endValue);CGFloat b = resultValue.startValue - (a * consultValue.startValue);return a * consule + b; }@end 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的IOS基础之愤怒的小方块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 史上最全换档讲解手动档汽车的换挡技巧
- 下一篇: 7款最流行的在线项目管理工具推荐