您的位置:首页 > 运维架构 > Tomcat

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

2015-09-03 11:10 495 查看
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 选中的下标

对应代码

//
//  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(0, 0, self.view.frame.size.width, self.view.frame.size.height-50)];
//    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(0, self.view.frame.size.height-50, self.view.frame.size.width, 50);
[_segment addTarget:self action:@selector(handleChange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_segment];
[_segment release];
}

//UISegmentedControl 控件点击触发事件
-(void)handleChange:(UISegmentedControl *)segment{
switch (segment.selectedSegmentIndex) {
case 0://进行生气
{
[self catActionWithImageName:@"angry" imageCount:25 imageIndex:0 soundName:@"angry" repeatCount:4 duration:4];
}
break;
case 1://进行喝牛奶
{
[self catActionWithImageName:@"drink" imageCount:80 imageIndex:0 soundName:@"p_drink_milk" repeatCount:4 duration:2];
}
break;
case 2://进行放屁
{
[self catActionWithImageName:@"fart" imageCount:27 imageIndex:0 soundName:@"fart001_11025" repeatCount:4 duration:3];
}
break;
case 3://进行踩左脚
{
[self catActionWithImageName:@"footLeft" imageCount:29 imageIndex:0 soundName:@"p_foot3" repeatCount:4 duration:3];
}
break;
case 4://
{
[self catActionWithImageName:@"eat" imageCount:39 imageIndex:0 soundName:@"p_eat" repeatCount:4 duration:3];
}
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 = 0; 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


View Code RootViewController.m

//随机出现一张图片

UIImageView * imageView1 = [[UIImageView alloc]initWithImage:imageArr[arc4random()%(10-0+1)+0]];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: