iOS-UIDynamicAnimator和UISnapBehavior动画示例(iOS打分动画)

    xiaoxiao2021-03-25  214

    1.前言

    应公司项目要求,我们有一个打分的功能,有十分制和百分制两种,主要用到UIDynamicAnimator和UISnapBehavior。UIkit动力学是UIkit框架中模拟真实世界的一些特性。主要有UIDynamicAnimator类,通过这个类中的不同行为来实现一些动态特性。UIDynamicAnimator有五个不同的行为,UIAttachmentBehavior(吸附),UICollisionBehavior(碰撞),UIGravityBehavior(重力),UIPushBehavior(推动),UISnapBehavior(捕捉)。另外还有一个辅助的行为UIDynamicItemBehavior,用来在item层级设定一些参数,比如item的摩擦,阻力,角阻力,弹性密度和可允许的旋转等等。这里面我主要说UISnapBehavior的用法。

    2.十分制

    调用非常简单,我封装了一下,如下:

    [ZFJScoreView showScoreView:ZFJExtraordinarySystem scorBtnAction:^(CGFloat score) { NSLog(@"你打的分数 == %f",score); _scoreLab.text = [NSString stringWithFormat:@"您打了%.0f分",score]; }]; 主要代码

    我创建了十个UIButton,给他们设置一个初始frame,然后通过UISnapBehavior设置一个CGPoint点;

    - (void)createMenu{ UIDynamicAnimator *animator = [[UIDynamicAnimator alloc]initWithReferenceView:self]; //#16E4B6 100% for (int i = 0; i<10; i++) { UIButton *scoreBtn = [[UIButton alloc]init]; scoreBtn.frame = CGRectMake((ScreenWidth - BtnWidth)/2, ScreenHeight - BtnWidth, BtnWidth, BtnWidth); [scoreBtn setTitle:[NSString stringWithFormat:@"%d",10-i] forState:UIControlStateNormal]; scoreBtn.titleLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:18]; scoreBtn.backgroundColor = [self getScoreColor:i]; scoreBtn.layer.masksToBounds = YES; scoreBtn.layer.cornerRadius = BtnWidth/2; [self addSubview:scoreBtn]; [scoreBtn addTarget:self action:@selector(scoreBtnClick:) forControlEvents:UIControlEventTouchUpInside]; CGFloat pointX = KBtnSpace + BtnWidth/2 + (KBtnSpace + BtnWidth) * (i%5); CGFloat pointY = ScreenHeight/2 - BtnWidth/2 - KBtnSpace/2 + (KBtnSpace/2 + BtnWidth) * (i/5); CGPoint point = CGPointMake(pointX, pointY); UISnapBehavior *snap = [[UISnapBehavior alloc]initWithItem:scoreBtn snapToPoint:point]; [animator addBehavior:snap]; } self.animator = animator; }

    3.百分制

    调用和十分制是一样的;

    [ZFJScoreView showScoreView:ZFJPercentileSystem scorBtnAction:^(CGFloat score) { NSLog(@"你打的分数 == %f",score); _scoreLab.text = [NSString stringWithFormat:@"您打了%.0f分",score]; }]; 百分制的实现代码

    #pragma mark - 以下是百分制 //#EF5F20 100% - (void)createPercentileSystem{ UIDynamicAnimator *animator = [[UIDynamicAnimator alloc]initWithReferenceView:self]; UIView *PercentileView = [[UIButton alloc]init]; PercentileView.frame = CGRectMake((ScreenWidth - KPercentileWID)/2, (ScreenHeight - KPercentileWID), KPercentileWID, KPercentileWID); PercentileView.backgroundColor = [UIColor colorWithRed:0.937 green:0.373 blue:0.125 alpha:1.00]; PercentileView.layer.masksToBounds = YES; PercentileView.layer.cornerRadius = KPercentileWID/2; [self addSubview:PercentileView]; [PercentileView addSubview:self.ScoreTextField]; //满分100分 UILabel *showLab = [[UILabel alloc]init]; showLab.frame = CGRectMake((KPercentileWID - 115)/2, CGRectGetMaxY(self.ScoreTextField.frame) + 9, 115, 13); showLab.text = @"满分100分"; showLab.textAlignment = NSTextAlignmentCenter; showLab.font = [UIFont fontWithName:@"STHeitiSC-Light" size:13]; showLab.textColor = [UIColor whiteColor]; [PercentileView addSubview:showLab]; CGFloat pointX = (ScreenWidth - KPercentileWID)/2 + KPercentileWID/2; CGFloat pointY = (ScreenHeight - KPercentileWID)/2 + KPercentileWID/2; CGPoint point = CGPointMake(pointX, pointY); UISnapBehavior *snap = [[UISnapBehavior alloc]initWithItem:PercentileView snapToPoint:point]; snap.damping = 0.8f;//剧烈程度 [animator addBehavior:snap]; self.animator = animator; }

    4.效果预览

    5.DEMO下载

    点击下载

    http://download.csdn.net/detail/u014220518/9773882

    转载请注明原文地址: https://ju.6miu.com/read-733.html

    最新回复(0)