毛玻璃效果
//1.加毛玻璃
UIToolbar *toolbar = [[UIToolbar alloc] init];
//2.设置frame
toolbar.frame = self.bgImageView.bounds;
//3.设置样式和透明度
toolbar.barStyle = UIBarStyleBlack;
toolbar.alpha = 0.98;
//4.加到背景图片上
[self.bgImageView addSubview:toolbar];
UIImageView的fram设置
- 方式一
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:@"1"];
imageView.frame = CGRectMake(100, 100, 267, 400);
imageView.frame = (CGRect){{100,100},{100,150}};
[self.view addSubview:imageView];
- 方式二
UIImageView *imageView = [[UIImageView alloc] init];
UIImage *image = [UIImage imageNamed:@"1"];
imageView.frame = CGRectMake(100, 100, image.size.width, image.size.height);
imageView.image = image;
[self.view addSubview:imageView];
- 方式三
UIImage *image = [UIImage imageNamed:@"1"];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 167, 300)];
imageView.image = image;
[self.view addSubview:imageView];
- 方式四
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];
imageView.center = CGPointMake(self.view.frame.size.width * 0.5, self.view.frame.size.height * 0.5);
[self.view addSubview:imageView];
UIImageView的帧动画
- 1.加载所有图片
NSMutableArray<UIImage *> *imageArr = [NSMutableArray array];
for (int i = 0; i < 20; ++i) {
//获得图片名称
NSString *imageName = [NSString stringWithFormat:@"%d",i+1];
//创建UImage对象
UIImage *image = [UIImage imageNamed:imageName];
//加入数组
[imageArr addObject:image];
}
- 2.设置动画图片
self.imageView.animationImages = imageArr;
- 3.设置动画的播放次数
self.imageView.animationRepeatCount = 0;
- 4.设置动画播放时长
self.imageView.animationDuration = 1.0;
- 5.开始动画
[self.imageView startAnimating];
- 6.停止动画
[self.imageView stopAnimating];
Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘* -[__NSArrayM insertObject:atIndex:]: object cannot be nil’
拳皇动画
- 0.加载图片的方法
-(NSArray*)loadAllImagesWithimagePrefix:(NSString*)
imagePrefix count:(int)count
{
NSMutableArray<UIImage *> *imageArr = [
NSMutableArray array];
for (int i = 0; i < count; ++i) {
//获得图片名称
NSString *imageName = [NSString
stringWithFormat:@"%@_%d",imagePrefix,i+1];
//创建UIImage对象
UIImage *image = [UIImage imageNamed:imageName];
//加入数组
[imageArr addObject:image];
}
return imageArr;
}
- 1.加载站立的所有图片
self.standImageArr = [self
loadAllImagesWithimagePrefix:@"stand" count:10];
- 2.加载小招的所有图片
self.smillSkillImageArr = [self
loadAllImagesWithimagePrefix:@"xiaozhao3" count:39];
- 3.加载大招的所有图片
self.bigSkillImageArr = [self
loadAllImagesWithimagePrefix:@"dazhao" count:87];
- 4.站立按钮的点击事件
-(IBAction)stand{
//设置图片动画
self.imageView.animationImages = self.standImageArr;
//设置动画的播放次数
self.imageView.animationRepeatCount = 0;
//设置动画播放时长
self.imageView.animationDuration = 1;
//开始动画
[self.imageView startAnimating];
}
- 5.小招按钮的点击事件
-(IBAction)smallSkill{
//设置图片动画
self.imageView.animationImages = self.smillSkillImageArr;
//设置动画的播放次数self.imageView.animationRepeatCount = 1;
//设置动画播放时长
self.imageView.animationDuration = 2.5;
//开始动画[self.imageView startAnimating];
//站立(延迟执行)
// Selector 方法
// Object 参数
// afterDelay 时间
[self performSelector:@selector(stand) withObject:nil afterDelay:self.imageView.animationDuration];
}
- 6.大招按钮的点击事件
- (IBAction)bigSkill{
//设置图片动画
self.imageView.animationImages = self.bigSkillImageArr;
//设置动画的播放次数
self.imageView.animationRepeatCount = 1;
//设置动画播放时长
self.imageView.animationDuration = 4;
//开始动画
[self.imageView startAnimating];
//站立(延迟执行)
// Selector 方法
// Object 参数
// afterDelay 时间
[self performSelector:@selector(stand) withObject:nil afterDelay:self.imageView.animationDuration];
}
拳皇动画的内存优化
- 1.重新设置图片的加载方式
/*图片的两种加载方式:
1> imageNamed:
a. 就算指向它的指针被销毁,该资源也不会被从内存中干掉
b. 放到Assets.xcassets的图片,默认就有缓存
c. 图片经常被使用
2> imageWithContentsOfFile:
a. 指向它的指针被销毁,该资源会被从内存中干掉
b. 放到项目中的图片就不由缓存
c. 不经常用,大批量的图片*/
NSString *imagePath = [[NSBundle mainBundle]
pathForResource:imageName ofType:@"png"];
UIImage *image = [UIImage
imageWithContentsOfFile:imagePath];
- 2.增加游戏结束按钮并设置点击事件
- (IBAction)gameOver {
//将强指针指向的内存置为空
self.standImageArr = nil;
self.smillSkillImageArr = nil;
self.bigSkillImageArr = nil;
self.imageView.animationImages = nil;
}
IOS播放音效
需要导入框架#import
@property (nonatomic, strong) AVPlayer *player;
- 2.创建播放器
//1 资源的URL地址
NSURL *url = [[NSBundle mainBundle]
URLForResource:@"mySong1.mp3" withExtension:nil];
//2 创建播放器曲目
AVPlayerItem *playItem = [[AVPlayerItem
alloc] initWithURL:url];
//3 创建播放器
self.player = [[AVPlayer alloc]
initWithPlayerItem:playItem];
- 3.播放音效
[self.player play];
拳皇中播放音效
- 1.在界面被加载时创建播放音乐器
//创建播放器
self.player = [[AVPlayer alloc] init];
- 2.播放音乐
//播放
NSURL *url = [[NSBundle mainBundle] URLForResource:musicName withExtension:nil];
AVPlayerItem *playItem = [[AVPlayerItem alloc] initWithURL:url];
[self.player replaceCurrentItemWithPlayerItem:playItem];
[self.player play];
self.player.rate = 1.5;