【完整APP】SpriteKit引擎开发iOS小游戏之五(移动端网络与优化)【完结】

本文介绍了在iOS游戏开发中如何利用SpriteKit进行网络请求和优化用户体验。通过自定义Loading和Toast类提升交互体验,使用NSURLSession进行网络请求,包括其优势和下载任务的管理。此外,还探讨了其他性能优化措施,如主线程保护、内存管理和后台任务处理,以及游戏未来可能的扩展方向。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【网络动画效果】

众所周知,包括但不限于网络处理,很多使用APP的时机都需要展示Loading或者Toast提示的形式来提升用户的交互体验。

  1. 自定义Loading类:是继承UIActivityIndicatorView的子类。简化创建与管理。指定了布局与样式等。对外暴露创建与消失方法。
#import "LJZLoading.h"

@implementation LJZLoading

- (instancetype)initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style
{
   
    self = [super initWithActivityIndicatorStyle:style];
    if(self){
   
        self.hidesWhenStopped = YES;
        self.frame = CGRectMake(0, 0, 150, 150);
        self.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2);
        self.backgroundColor = [UIColor blackColor];
        self.alpha = 0.5;
        [[[UIApplication sharedApplication] keyWindow] addSubview:self];
        [self startAnimating];
    }
    return self;
}

- (void)StopShowLoading
{
   
    [self stopAnimating];
    [self removeFromSuperview];
}

@end
  1. 自定义Toast类”LJZToast“:设计为以单例模式调用,控制内容与展示时长,在固定位置展示出提示信息。其头文件如下:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface ToastLabel : UILabel
- (void)setMessageText:(NSString *)text;
@end

@interface LJZToast : NSObject
@property (nonatomic,strong)ToastLabel *toastLabel;

+ (instancetype)shareInstance;
- (void)ShowToast:(NSString *)message duration:(CGFloat)duration;

@end
  1. Toast的实现上,UILabel子类的样式自定义,dispatch_once执行静态单例。dispatch_after控制展示animateWithDuration实现消失动画,具体实现如下:
#import "LJZToast.h"
@implementation ToastLabel

- (instancetype)init
{
   
    self = [super init];
    if(self){
   
        self.layer.cornerRadius = 8;
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor blackColor];
        self.numberOfLines = 0;
        self.textAlignment = NSTextAlignmentCenter;
        self.textColor = [UIColor whiteColor];
        self.font = [UIFont systemFontOfSize:15];
    }
    return self;
}

- (void)setMessageText:(NSString *)text
{
   
    [self setText:text];
    self.frame = CGRectMake(0, 0, 150, 50);
    self.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, 150);
}

@end


@implementation LJZToast

+ (instancetype)shareInstance
{
   
    static LJZToast *singleton = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
   
        singleton = [[LJZToast alloc] init];
    });
    return singleton
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值