标签:des style blog class code java
一、需要一个新的控制器(ReadyViewController)
//2.4为stageView添加手势监听 [stageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemClick:)]];
@property (nonatomic, copy) void (^itemClickBlock)(StageInfo *stageInfo);
//3.跳转到ReadyViewController的block方法 stageView.itemClickBlock = ^(StageInfo *stageInfo) { [self performSegueWithIdentifier:@"ready" sender:stageInfo]; };
#pragma mark -view点击的手势监听的方法 - (void)itemClick:(UIGestureRecognizer *)tap { if (_itemClickBlock != nil) { Stage *stageView = (Stage*)tap.view; _itemClickBlock(stageView.stageInfo); } }
//关卡信息 @property (nonatomic, strong) StageInfo *stageInfo;
#pragma mark - 跳转到ReadyViewController的过程会执行的方法 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { ReadyViewController *ready = segue.destinationViewController; ready.stageInfo = (StageInfo*)sender; }
注明:整个过程实现了将StageInfo从Stage中,一直传递到准备开始游戏的控制器中(ReadyViewController),整个传递过程是
Stage--->StageView--->StageViewController--->ReadyViewController
//2.加载关卡的label _stageTitle.text = [NSString stringWithFormat:@"Stage %d", _stageInfo.stageNo]; _stageIntro.text = [_stageInfo.intro stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
#pragma mark - 重写setStageInfo对象 - (void)setStageInfo:(StageInfo *)stageInfo { [self setFullView:@"select_bg.jpg"]; _stageInfo = stageInfo; //0.播放声音 [[SoundManager sharedSoundManager] playSound:kSoundReady]; //1.设置准备界面的标题 _stageReadyTitle.text = [NSString stringWithFormat:@"STAGE %d", stageInfo.stageNo]; //2.设置几个label的文字 NSArray *strings = [stageInfo.title componentsSeparatedByString:@"\\n"]; int count = _labels.count; CGFloat timeCount = 0; for (int i = 0; i<count; i++) { UILabel *label = _labels[i]; label.text = strings[i]; label.hidden = YES; CGFloat delay = 0.3 * (i + 1); timeCount += delay; label.tag = i + 1; [NSTimer scheduledTimerWithTimeInterval:delay target:self selector:@selector(showLabel:) userInfo:label repeats:NO]; } //3.设置一个定时器,移除自身的View [NSTimer scheduledTimerWithTimeInterval:timeCount target:self selector:@selector(removeView) userInfo:nil repeats:NO]; } #pragma mark 显示label的动画 - (void)showLabel:(NSTimer *)timer { //1.取出label并且设置不隐藏 UILabel *label = timer.userInfo; label.hidden = NO; //2.添加动画到涂层 CAAnimationGroup * group = [self animationCroup]; [label.layer addAnimation:group forKey:nil]; //3.播放声音 [[SoundManager sharedSoundManager] playSound:kDropTitle(label.tag)]; } - (CAAnimationGroup *)animationCroup { //1.伸缩动画 CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"]; scale.fromValue = @0; scale.toValue = @1; //2.位移动画 CAKeyframeAnimation *trans = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"]; trans.values = @[@0, @40, @20]; //3.添加动画组 CAAnimationGroup *group = [CAAnimationGroup animation]; group.animations = @[scale, trans]; return group; } #pragma mark 移除View - (void)removeView { [self removeFromSuperview]; _removeBlock(); }
@property (nonatomic, copy) NSString *format;
@property (nonatomic, copy) NSString *unit;
- (void)setStageInfo:(StageInfo *)stageInfo { _stageInfo = stageInfo; //1.确定每一个label显示的值 CGFloat margin = (stageInfo.max - stageInfo.min)/5; int count = _labels.count; for (int i = 0; i<count; i++) { UILabel *label = _labels[i]; CGFloat score = stageInfo.max - i * margin; label.text = [NSString stringWithFormat:stageInfo.format, score]; } //2.显示计分器的位置 [self setScoreHintCenter:margin]; if (!_stageInfo.stageRecordModel.rank) { _scoreHint.hidden = YES; return; } //3.执行动画 self.transform = CGAffineTransformMakeTranslation(0, 200); [UIView animateWithDuration:0.3 animations:^{ //平移动画(CGAffineTransformIdentity表示清空所有的属性) self.transform = CGAffineTransformIdentity; } completion:^(BOOL finished) { CAKeyframeAnimation *blink = [CAKeyframeAnimation animationWithKeyPath:@"hidden"]; blink.values = @[@0, @1, @0]; blink.repeatCount = 3; [_scoreHint.layer addAnimation:blink forKey:nil]; }]; } #pragma mark 设置指示器的位置 - (void)setScoreHintCenter:(CGFloat)margin { CGPoint center = _scoreHint.center; // 计算现在积分 和 最差成绩 的差距 CGFloat delta = _stageInfo.stageRecordModel.score - _stageInfo.min; // 计算 每一分 占据 多少宽度 CGFloat widthPerScore = center.x / (margin * 6); // 根据 积分差距 和 每分占据的宽度 来计算总宽度差距 center.x -= delta * widthPerScore; if (center.x <= 0) { center.x = 0; } _scoreHint.center = center; }
_stageReadyView.removeBlock = ^{ _stageScoreView.stageInfo = _stageInfo; };
#pragma mark 移除View - (void)removeView { [self removeFromSuperview]; _removeBlock(); }
应用程序开发之模仿史上最牛游戏(四),布布扣,bubuko.com
标签:des style blog class code java
原文地址:http://www.cnblogs.com/letougaozao/p/3721429.html