鱼模块的代理方法
创建一个被钓到的鱼,加在鱼钩上,这样便可和鱼钩一起执行上钩动画了
#pragma mark - FishModelImageViewDelegate 钓到鱼后的代理
- (void)catchTheFishWithType:(FishModelImageViewType)type andDirection:(FishModelImageViewDirection)dir andWinCount:(int)count{
self.isCatched = YES;
FishModelImageView *fishImageView = [[FishModelImageView alloc] initCatchedFishWithType:type andDirection:dir];
[self.fishHookView addSubview:fishImageView];
fishImageView.y = fishImageView.y + _catchedHeight;
_catchedHeight += 8;//每钓到一个y坐标往下移
//赢得钱数
self.winMoney += count;
}
2.初始化鱼塘
简单的创建鱼背景并添加点击手势
3.初始化鱼
通过for循环可以创建出多个某种鱼
//小黄鱼
for (int i = 0; i < 8; i++){
FishModelImageView *model1 = [[FishModelImageView alloc] initCanCatchFishWithType:FishModelImageViewTypeXHY andDirection: (i%2 == 0) ? FishModelImageViewFromRight : FishModelImageViewFromLeft];
model1.delegate = self;
[self.bgImageView addSubview:model1];
}
4.资源移除
由于计时器不销毁会造成循环引用,导致内存泄漏,所以必须手动移除他,还有动画如果执行了代理,并且设置了结束后停留在结束位置,也会得不到释放,所以都要手动释放资源
- (void)removeFishViewResource{
//解决鱼钩上钩动画循环引用的问题
_linePathLayer = nil;
//钓鱼计时器关闭
[_fishTimer invalidate];
_fishTimer = nil;
//释放鱼钩的计时器
[self.fishHookView hoolTimerInvalidate];
//发送通知释放小鱼资源
[[NSNotificationCenter defaultCenter] postNotificationName:NotificationRemoveFishModelTimer object:nil];
}
总结
至此,本游戏已经完成了,写的比较多,也比较乱,有什么不好的地方欢迎批评指正,希望对大伙有所帮助吧,本demo地址【https://github.com/Ccalary/FishingGame】
注:相关教程知识阅读请移步到IOS开发频道。










