UI:tomcat(说话小程序)、相框动画、UISgmentcontrol、UISwitch

时间:2023-02-08 20:36:20

UISegmentedControl 分段控件

//1. UISegmentedControl 分段控件 (就是一个一个的按钮)

//分段显示的标题是以一个数组存储的

NSArray * titles = @[@"护卫队",@"三军仪仗队",@"步兵队"];

UISegmentedControl * segment = [[UISegmentedControl alloc]initWithItems:titles];

segment.frame = CGRectMake(20, 40, 335, 40);//指定所在视图位置

segment.tintColor = [UIColor redColor];//设置选中的颜色

//设置默认的选中项目

segment.selectedSegmentIndex = 1;

//添加点击事件 (点击哪一个就触发哪一个的下标值对应的事件)

[segment addTarget:self action:@selector(handleSegment:) forControlEvents:UIControlEventValueChanged];

//设置某项的 title

[segment setTitle:@"女兵方队" forSegmentAtIndex:2];

//设置某项的特定宽度 (一旦设定,只改变当前的,其他的仍旧等分)

[segment setWidth:100 forSegmentAtIndex:2];

//设置文字大小 颜色

NSDictionary * dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor blueColor]};

//    segment setTitleTextAttributes:<#(NSDictionary *)#> forState:<#(UIControlState)#>

[segment setTitleTextAttributes:dic forState:UIControlStateNormal];

[self.view addSubview:segment];

[segment release];

segment.selectedSegmentIndex 选中的下标

对应代码

 NSArray * titles = @[@"护卫队",@"三军仪仗队",@"步兵队",@"炮兵分队"];
UISegmentedControl * segment = [[UISegmentedControl alloc]initWithItems:titles];
segment.frame = CGRectMake(, , , );//指定所在视图位置
//添加点击事件 (点击哪一个就触发哪一个的下标值对应的事件)
[segment addTarget:self action:@selector(handleSegment:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segment];
[segment release]; }
//点击事件
-(void)handleSegment:(UISegmentedControl *)segment{
switch (segment.selectedSegmentIndex) {
case :
{
NSLog(@"开始阅兵-护卫队");
}
break;
case :
{
NSLog(@"开始阅兵-三军仪仗队");
}
break;
case :
{
NSLog(@"开始阅兵-步兵队");
}
break;
case :
{
NSLog(@"开始阅兵-炮兵分队");
}
break;
default:
break;
}

UIsegmentedContro  demo

//2.滑块控件 UISlider (用于音频、视频的快进,倒退)

//2.滑块控件 UISlider (用于音频、视频的快进,倒退)

//是一个滑竿 存放着一系列连续的值

UISlider * slider = [[UISlider alloc]initWithFrame:CGRectMake(30, 100, 300, 30)];

//设置滑竿的最小值,最大值

slider.minimumValue = 0.0;

slider.maximumValue = 1.0;

//添加滑动时的触发事件

[slider addTarget:self action:@selector(handleSlider:) forControlEvents:UIControlEventValueChanged];

[self.view addSubview:slider];

[slider release];

//设置滑块的显示图片(不用系统提供的小白点)

[slider setThumbImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];

//在根视图上放一个 View

UIView * View = [[UIView alloc]initWithFrame:CGRectMake(80, 180, 100, 100)];

View.tag = 101;

//设置划过区域的滑竿颜色

slider.minimumTrackTintColor = [UIColor redColor];

//设置未划过区域的滑竿的颜色

slider.maximumTrackTintColor = [UIColor blueColor];

//滑块的默认位置

slider.value = 0.3;

//    View.hidden = YES;

View.backgroundColor = [UIColor greenColor];

[self.view addSubview:View];

[View release];

上面的例子对应的实现:

//滑动时的触发事件
-(void)handleSlider:(UISlider *)slider{
NSLog(@"%f",slider.value);
UIView *aView = [[UIView alloc]init];
aView = [self.view viewWithTag:];
// self.view.hidden = NO;
// self.view.alpha = slider.value;
aView.alpha = slider.value;
//获取 View 的背景颜色的 RGB 值
CGFloat red = 0.0;
CGFloat green = 0.0;
CGFloat blue = 0.0;
[aView.backgroundColor getRed:&red
green:&green
blue:&blue
alpha:nil];
NSLog(@"打印 RGB 值; red %f, green %f, blue %f",red,green,blue);
}

对应的实现

在相框类 UIImageView 对象里添加图片的两种方式:

(1)给定图片的名字

(2)通过路径找到文件,例如做动态图的时候

UIimage * image = [UIimage imageNamed:@"图片名字"];

NSString * filePath = [[NSBundle mainBundle] pathForResource:"文件名"]//获取文件路径

NSString * image = [UIimage imageWithContentsOfFile:文件路径];//寻找相应的文件

图片按照缩放比呈现在相框里

CGFloat width = image.size.width;//获取当前图片的尺寸

CGFloat height = image.size.height;

UIImageView * imageView = [[UIImageView alloc]initWithImage:image];//把相片载入相框

imageView.frame = CGRectMake = (x,y,w*width,h*height);//同比适应图片大小(等比例缩放)

[self.view addSubview:imageView];

在相框框中加载动态图片

animationImages 是一个数组(存放动态图片的)

设置相框  UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 275, 200)];

NSMutableArray * imageArr = [[NSMutableArray alloc]initWithCapacity:0];用于获取动态图片的数组

//获取图片的路径(一般图片都是有规律的名字,如 a1...a10 如果不安规律命名 就要一张一张的获取)

for (int i = 0; i< 6; i++) {

NSString * imageName = [NSString stringWithFormat:@"flag%d",i+1];

//        NSString * imageName =

NSString * filePath = [[NSBundle mainBundle]pathForResource:imageName ofType:@"tiff"];

UIImage * image = [UIImage imageWithContentsOfFile:filePath];

//把图片存放到数组中

[imageArr addObject:image];

}

imageView.animationImages = imageArr;

设置播放时间0.3秒  imageView.animationDuration = 0.3,

播放次数  imageView.animationRepeatCount = 0;//给 0 的话是无限的播放,我们的默认是无限

//开启播放

[imageView startAnimating];

[self.view addSubview:imageView];

#import "AppDelegate.h"
#import "WindowRootViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible]; WindowRootViewController * RootVC = [[WindowRootViewController alloc]init];
self.window.rootViewController = RootVC;
[RootVC release]; return YES;
}

AppDelegate.m 文件

//
// WindowRootViewController.m #import "WindowRootViewController.h" @interface WindowRootViewController () @end @implementation WindowRootViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//1.分段控件 UISegemnetedControl 控件
//
NSArray * titles = @[@"护卫队",@"三军仪仗队",@"步兵队",@"炮兵分队"];
UISegmentedControl * segment = [[UISegmentedControl alloc]initWithItems:titles];
segment.frame = CGRectMake(, , , );//指定所在视图位置
//添加点击事件 (点击哪一个就触发哪一个的下标值对应的事件)
[segment addTarget:self action:@selector(handleSegment:) forControlEvents:UIControlEventValueChanged];
//设置选中的颜色
segment.tintColor = [UIColor redColor];
//设置默认的选中项目
segment.selectedSegmentIndex = ;
//设置某项的 title
[segment setTitle:@"女兵方队" forSegmentAtIndex:];
//设置某项的特定宽度,(其他几项等分)
[segment setWidth: forSegmentAtIndex:];
//设置文字大小 颜色
NSDictionary * dic = @{NSFontAttributeName:[UIFont systemFontOfSize:],NSForegroundColorAttributeName:[UIColor blueColor]};
// segment setTitleTextAttributes:<#(NSDictionary *)#> forState:<#(UIControlState)#>
[segment setTitleTextAttributes:dic forState:UIControlStateNormal];
[self.view addSubview:segment];
[segment release]; /*
//2.滑块控件 UISlider (用于音频、视频的快进,倒退)
//是一个滑竿 存放着一系列连续的值
UISlider * slider = [[UISlider alloc]initWithFrame:CGRectMake(30, 100, 300, 30)];
//设置滑竿的最小值,最大值
slider.minimumValue = 0.0;
slider.maximumValue = 1.0;
//添加滑动时的触发事件
[slider addTarget:self action:@selector(handleSlider:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider];
[slider release];
//设置滑块的显示图片(不用系统提供的小白点)
[slider setThumbImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
//在根视图上放一个 View
UIView * View = [[UIView alloc]initWithFrame:CGRectMake(80, 180, 100, 100)];
View.tag = 101;
//设置划过区域的滑竿颜色
slider.minimumTrackTintColor = [UIColor redColor];
//设置未划过区域的滑竿的颜色
slider.maximumTrackTintColor = [UIColor blueColor];
//滑块的默认位置
slider.value = 0.3;
// View.hidden = YES;
View.backgroundColor = [UIColor greenColor];
[self.view addSubview:View];
[View release]; // UISlider * slider2 = [[UISlider alloc]initWithFrame:CGRectMake(50, 50, 320, 20)];
// slider2.ma */ //上午第二节 (两种方式的区别)
/*
//图片类 UIImageView <加载静态图>
//第一种方式,给定图片的名字
// UIImage * image = [UIImage imageNamed:@"12.jpg"];//用了便利构造器,不用释放
//第二种 按照给定图片(所在应用包里)的路径
//NSBundle 是应用程序的包(如果做应用程序 图片资源都会打包放到里面) resource 是资源的名字 type 就是资源的类型(就是后缀名)
//NSBundle 可以理解为一个存放程序使用到的 音频 视频 的文件夹
NSString * filePath = [[NSBundle mainBundle]pathForResource:@"12" ofType:@"jpg"];
UIImage * image = [UIImage imageWithContentsOfFile:filePath];//去寻找应用文件
//压缩图片
//获取 image 的大小
CGFloat width = image.size.width;
CGFloat height = image.size.height;
//UIImageView 相框
//根据图片大小设置相框
UIImageView * imageView = [[UIImageView alloc]initWithImage:image];
imageView.frame = CGRectMake(80, 110, 200, 200*height/width);//同比适应图片大小(等比例缩放)
[self.view addSubview:imageView];
[imageView release];
*/ //加载动态图片 //获取图片
//加载动态图片
UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(, , , )];
//设置 imageView 播放的一组动态图片
// animationImages 是一个数组
//获取图片
NSMutableArray * imageArr = [[NSMutableArray alloc]initWithCapacity:];
//获取图片的路径(一般图片都是有规律的名字,如 a1...a10 如果不安规律命名 就要一张一张的获取)
for (int i = ; i< ; i++) {
NSString * imageName = [NSString stringWithFormat:@"flag%d",i+];
// NSString * imageName =
NSString * filePath = [[NSBundle mainBundle]pathForResource:imageName ofType:@"tiff"];
UIImage * image = [UIImage imageWithContentsOfFile:filePath];
//把图片存放到数组中
[imageArr addObject:image];
}
imageView.animationImages = imageArr;
//设置播放时间
imageView.animationDuration = 0.3;
imageView.animationRepeatCount = ;//给 0 的话是无限的播放,我们的默认是无限
//开启播放
[imageView startAnimating]; [self.view addSubview:imageView];
}
//点击事件
-(void)handleSegment:(UISegmentedControl *)segment{
switch (segment.selectedSegmentIndex) {
case :
{
NSLog(@"开始阅兵-护卫队");
}
break;
case :
{
NSLog(@"开始阅兵-三军仪仗队");
}
break;
case :
{
NSLog(@"开始阅兵-步兵队");
}
break;
case :
{
NSLog(@"开始阅兵-炮兵分队");
}
break;
default:
break;
} }
//滑动时的触发事件
-(void)handleSlider:(UISlider *)slider{
NSLog(@"%f",slider.value);
UIView *aView = [[UIView alloc]init];
aView = [self.view viewWithTag:];
// self.view.hidden = NO;
// self.view.alpha = slider.value;
aView.alpha = slider.value;
//获取 View 的背景颜色的 RGB 值
CGFloat red = 0.0;
CGFloat green = 0.0;
CGFloat blue = 0.0;
[aView.backgroundColor getRed:&red
green:&green
blue:&blue
alpha:nil];
NSLog(@"打印 RGB 值; red %f, green %f, blue %f",red,green,blue);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end

WindowRootViewController.m 文件


小总结:

获取某类型的文件 [[NSBundle mainBundle]pathForResource:文件名字 ofType:文件类型或者文件后缀名]

//        NSString * path = [[NSBundle mainBundle]pathForResource:<#(NSString *)#> ofType:<#(NSString *)#>];

相框的等比例加载图片

相框 UIImageView 类的使用

动态图片的加载 (动态图的开始播放 satrAnimating      结束播放 stopAnimating  播放时间 animationDuration      播放次数 animationCount  )

aView = [self.view viewWithTag:101];根据标签得到视图,不能写成了为临时视图标签赋值了


UIStepper

UISwitch 控件 (开关控件)

继承关系

UISwitch :UIControl : UIView : UIResponder : NSObject

如果想让一个视图自带一个点击事件的话,让视图继承UIControl即可。

UISwitch 还有 UIScreenEdgePanGestureRecongnizer 都是IOS7之后才有的新的特性,对于 UISwitch 我们可是使用 UIButton 去实现

UISwitch * sw = [UISwitch alloc]initWitchFrameMake:CGRectMake(x,y,w,h)];

开关属性 sw.on 默认为关闭状态

sw.on = YES;

点击时候的颜色 tintColor

sw.tintColor = [UIColor redColor];

开启时候的颜色 onTintColor  sw.onTintColor = 颜色设置

thumbColor 属性是按钮的那个点的颜色  sw.thumbColor = [UIColor redColor];

设置点击方法事件  sw addTarget:id action:(SEL) forControlEVents:(对应状态)

设置开启状态下的图片 sw.onImage =[UIImage imageNamed: ] 图片设置

代码:

//
// WindowRootViewController.m #import "WindowRootViewController.h" @interface WindowRootViewController ()
@property(nonatomic,retain)UIImageView *imageView;
@end @implementation WindowRootViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//1.分段控件 UISegemnetedControl 控件
//
NSArray * titles = @[@"护卫队",@"三军仪仗队",@"步兵队",@"炮兵分队"];
UISegmentedControl * segment = [[UISegmentedControl alloc]initWithItems:titles];
segment.frame = CGRectMake(, , , );//指定所在视图位置
//添加点击事件 (点击哪一个就触发哪一个的下标值对应的事件)
[segment addTarget:self action:@selector(handleSegment:) forControlEvents:UIControlEventValueChanged];
//设置选中的颜色
segment.tintColor = [UIColor redColor];
//设置默认的选中项目
segment.selectedSegmentIndex = ;
//设置某项的 title
[segment setTitle:@"女兵方队" forSegmentAtIndex:];
//设置某项的特定宽度,(其他几项等分)
[segment setWidth: forSegmentAtIndex:];
//设置文字大小 颜色
NSDictionary * dic = @{NSFontAttributeName:[UIFont systemFontOfSize:],NSForegroundColorAttributeName:[UIColor blueColor]};
// segment setTitleTextAttributes:<#(NSDictionary *)#> forState:<#(UIControlState)#>
[segment setTitleTextAttributes:dic forState:UIControlStateNormal];
[self.view addSubview:segment];
[segment release]; /*
//2.滑块控件 UISlider (用于音频、视频的快进,倒退)
//是一个滑竿 存放着一系列连续的值
UISlider * slider = [[UISlider alloc]initWithFrame:CGRectMake(30, 100, 300, 30)];
//设置滑竿的最小值,最大值
slider.minimumValue = 0.0;
slider.maximumValue = 1.0;
//添加滑动时的触发事件
[slider addTarget:self action:@selector(handleSlider:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider];
[slider release];
//设置滑块的显示图片(不用系统提供的小白点)
[slider setThumbImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
//在根视图上放一个 View
UIView * View = [[UIView alloc]initWithFrame:CGRectMake(80, 180, 100, 100)];
View.tag = 101;
//设置划过区域的滑竿颜色
slider.minimumTrackTintColor = [UIColor redColor];
//设置未划过区域的滑竿的颜色
slider.maximumTrackTintColor = [UIColor blueColor];
//滑块的默认位置
slider.value = 0.3;
// View.hidden = YES;
View.backgroundColor = [UIColor greenColor];
[self.view addSubview:View];
[View release]; // UISlider * slider2 = [[UISlider alloc]initWithFrame:CGRectMake(50, 50, 320, 20)];
// slider2.ma */ //上午第二节 (两种方式的区别)
/*
//图片类 UIImageView <加载静态图>
//第一种方式,给定图片的名字
// UIImage * image = [UIImage imageNamed:@"12.jpg"];//用了便利构造器,不用释放
//第二种 按照给定图片(所在应用包里)的路径
//NSBundle 是应用程序的包(如果做应用程序 图片资源都会打包放到里面) resource 是资源的名字 type 就是资源的类型(就是后缀名)
//NSBundle 可以理解为一个存放程序使用到的 音频 视频 的文件夹
NSString * filePath = [[NSBundle mainBundle]pathForResource:@"12" ofType:@"jpg"];
UIImage * image = [UIImage imageWithContentsOfFile:filePath];//去寻找应用文件
//压缩图片
//获取 image 的大小
CGFloat width = image.size.width;
CGFloat height = image.size.height;
//UIImageView 相框
//根据图片大小设置相框
UIImageView * imageView = [[UIImageView alloc]initWithImage:image];
imageView.frame = CGRectMake(80, 110, 200, 200*height/width);//同比适应图片大小(等比例缩放)
[self.view addSubview:imageView];
[imageView release];
*/
/* */
//加载动态图片 //获取图片
//加载动态图片
UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(, , , )];
imageView.tag = ;
//设置 imageView 播放的一组动态图片
// animationImages 是一个数组
//获取图片
NSMutableArray * imageArr = [[NSMutableArray alloc]initWithCapacity:];
//获取图片的路径(一般图片都是有规律的名字,如 a1...a10 如果不安规律命名 就要一张一张的获取)
for (int i = ; i< ; i++) {
NSString * imageName = [NSString stringWithFormat:@"flag%d",i+];
// NSString * imageName =
NSString * filePath = [[NSBundle mainBundle]pathForResource:imageName ofType:@"tiff"];
UIImage * image = [UIImage imageWithContentsOfFile:filePath];
//把图片存放到数组中
[imageArr addObject:image];
}
imageView.animationImages = imageArr;
//设置播放时间
imageView.animationDuration = 0.3;
imageView.animationRepeatCount = ;//给 0 的话是无限的播放,我们的默认是无限
//开启播放
_imageView =imageView;
// [imageView startAnimating]; [self.view addSubview:imageView]; //下午所学 UIStepper(自己查阅学习)
//UISwitch 开关控件 控制开关两种状态(比如系统里面打开wifi 的开关)在 IOS7 下是没有作用的 如果在IOS7 以下我们可以用 UIButton 去实现
UISwitch * sw = [[UISwitch alloc]initWithFrame:CGRectMake(, , , )];
sw.on = YES;//设置开关状态,默认是关闭状态
//改变颜色 (点击按钮时候的渲染颜色)
sw.tintColor = [UIColor greenColor];
//设置开启状态的渲染颜色
sw.onTintColor = [UIColor grayColor];
//设置 thumb 颜色
sw.thumbTintColor = [UIColor blackColor];
//设置点击方法 (凡是继承与 UIControl 的类 都可以有以下的添加点击事件的方法)
[sw addTarget:self action:@selector(handleSwitch:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:sw];
[sw release];
//设置不同(开启和关闭状态下)的图片
sw.onImage = [UIImage imageNamed:@""]; }
//点击事件
-(void)handleSegment:(UISegmentedControl *)segment{
switch (segment.selectedSegmentIndex) {
case :
{
NSLog(@"开始阅兵-护卫队");
}
break;
case :
{
NSLog(@"开始阅兵-三军仪仗队");
}
break;
case :
{
NSLog(@"开始阅兵-步兵队");
}
break;
case :
{
NSLog(@"开始阅兵-炮兵分队");
}
break;
default:
break;
} }
//滑动时的触发事件
-(void)handleSlider:(UISlider *)slider{
NSLog(@"%f",slider.value);
UIView *aView = [[UIView alloc]init];
aView = [self.view viewWithTag:];
// self.view.hidden = NO;
// self.view.alpha = slider.value;
aView.alpha = slider.value;
//获取 View 的背景颜色的 RGB 值
CGFloat red = 0.0;
CGFloat green = 0.0;
CGFloat blue = 0.0;
[aView.backgroundColor getRed:&red
green:&green
blue:&blue
alpha:nil];
NSLog(@"打印 RGB 值; red %f, green %f, blue %f",red,green,blue);
} //开关控件点击事件
-(void)handleSwitch:(UISwitch *)sw{
// switch ((int)sw.on) {
// case YES:
// {
// NSLog(@"开启");
// }
// break;
// case NO:
// {
// NSLog(@"关闭");
// }
// break;
// default:
// break;
// }
UIImageView * view = (UIImageView *)[self.view viewWithTag:];
if (sw.on) {
NSLog(@"开启");
[view startAnimating];//利用 tag 值
// [_imageView startAnimating];//利用属性控制
}
if (!sw.on) {
NSLog(@"关闭");
[view stopAnimating];
// [_imageView stopAnimating];
}
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end

代码


tomCat 实现代码:

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

AppDelegate。h

#import "AppDelegate.h"
#import "RootViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootViewController * RootVC = [[RootViewController alloc]init];
self.window.rootViewController = RootVC; return YES;
}

AppDelegate.m

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface RootViewController : UIViewController @end

RootViewController.h

//
// RootViewController.m #import "RootViewController.h" @interface RootViewController ()
@property(nonatomic,retain)UIImageView * imageView;
@property(nonatomic,retain)UISegmentedControl * segment;
@property(nonatomic,retain)AVAudioPlayer * player; @end @implementation RootViewController -(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setUpView];
} //布局页面
-(void)setUpView{
UIImage * image = [UIImage imageNamed:@"angry_00.jpg"];
self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height-)];
// CGFloat width = image.size.width;
// CGFloat height = image.size.height;
// [_imageView initWithImage:image];
[_imageView setImage:image];
[self.view addSubview:self.imageView];
[_imageView release];
NSArray * title = @[@"生气",@"喝奶",@"放屁",@"踩脚",@"吃饭"];
self.segment = [[UISegmentedControl alloc]initWithItems:title];
_segment.frame = CGRectMake(, self.view.frame.size.height-, self.view.frame.size.width, );
[_segment addTarget:self action:@selector(handleChange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_segment];
[_segment release];
} //UISegmentedControl 控件点击触发事件
-(void)handleChange:(UISegmentedControl *)segment{
switch (segment.selectedSegmentIndex) {
case ://进行生气
{
[self catActionWithImageName:@"angry" imageCount: imageIndex: soundName:@"angry" repeatCount: duration:];
}
break;
case ://进行喝牛奶
{
[self catActionWithImageName:@"drink" imageCount: imageIndex: soundName:@"p_drink_milk" repeatCount: duration:];
}
break;
case ://进行放屁
{
[self catActionWithImageName:@"fart" imageCount: imageIndex: soundName:@"fart001_11025" repeatCount: duration:];
}
break;
case ://进行踩左脚
{
[self catActionWithImageName:@"footLeft" imageCount: imageIndex: soundName:@"p_foot3" repeatCount: duration:];
}
break;
case ://
{
[self catActionWithImageName:@"eat" imageCount: imageIndex: soundName:@"p_eat" repeatCount: duration:];
}
break;
default:
break;
}
} //进行相应操作的方法
//NSTimeInterval 以秒为单位的时间属性
//@"%@_%02d" 保证后面的数字是数字个数不足两位的时候在前面补0
-(void)catActionWithImageName:(NSString *)imageName
imageCount:(NSInteger)count
imageIndex:(int)index
soundName:(NSString *)soundName
repeatCount:(NSInteger)repeatCount
duration:(NSTimeInterval)duration{
//播放动画效果
NSMutableArray * arr = [NSMutableArray arrayWithCapacity:count];
for (int i = index; i < count; i++) {
NSString * path = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%@_%02d",imageName,i] ofType:@"jpg"];//获取图片路径
UIImage * image = [UIImage imageWithContentsOfFile:path];//获取图片
[arr addObject:image];
}
self.imageView.animationImages = arr;
self.imageView.animationDuration = duration;
self.imageView.animationRepeatCount = repeatCount;
[self.imageView startAnimating];
//播放音效
NSString * filePath = [[NSBundle mainBundle]pathForResource:soundName ofType:@"m4a"];
//创建一个音频播放器对象
//initWithContentsOfURL:[NSURL URLWithString:filePath] 如果是网上的音频,需要改为网址
self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL URLWithString:filePath] error:nil];
for (int j = ; j < repeatCount; j++) {
//播放
[_player play];
}
[_player release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end

RootViewController.m

//随机出现一张图片

UIImageView * imageView1 = [[UIImageView alloc]initWithImage:imageArr[arc4random()%(10-0+1)+0]];