#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
#pragma mark ====== 主窗口 =======================
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
#pragma mark ====== 根视图控制器 =======================
self.window.rootViewController = [[RootViewController alloc]init];
[self.window addSubview:self.window.rootViewController.view];
return YES;
}
控制器:
#import "RootViewController.h"
//1.引入
#import "RootView.h"
@interface RootViewController ()
//2.声明私有属性
@property(nonatomic,strong) RootView *rootView;
@end
@implementation RootViewController
//3.用自定义视图 替换 控制器自带的view
-(void)loadView
{
self.rootView = [[RootView alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.view = self.rootView;
}
- (void)viewDidLoad {
[super viewDidLoad];
#pragma mark ===== segmentedControl 的事件 ===============
//UIControlEventValueChanged 当事件的值改变的时候。
[self.rootView.segmentedControl addTarget:self action:@selector(segmentedControlAction:) forControlEvents:UIControlEventValueChanged];
#pragma mark ========== slider 事件 ===================
[self.rootView.slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
}
//sliderAction:
-(void)sliderAction:(UISlider *)sender
{
//minimumValue
sender.minimumValue = 1;
//maximumValue
sender.maximumValue = 100;
//value 获取当前slider的值
NSLog(@"%.2f",sender.value);
//continuous 不输出滑动过程中的值
sender.continuous = NO;
}
//segmentedControlAction
-(void)segmentedControlAction:(UISegmentedControl *)sender
{
//selectedSegmentIndex 获取segmentedControl的索引值。
// NSLog(@"%ld", sender.selectedSegmentIndex);
if (sender.selectedSegmentIndex == 0) {
[self.rootView bringSubviewToFront:self.rootView.yidong];
}else if (sender.selectedSegmentIndex == 1)
{
[self.rootView bringSubviewToFront:self.rootView.liantong];
}else
{
[self.rootView bringSubviewToFront:self.rootView.dianxin];
}
}
视图:
#import <UIKit/UIKit.h>
@interface RootView : UIView
@property(nonatomic,strong) UISegmentedControl *segmentedControl;
@property(nonatomic,strong) UIImageView *yidong;
@property(nonatomic,strong) UIImageView *liantong;
@property(nonatomic,strong) UIImageView *dianxin;
@property(nonatomic,strong) UISlider *slider;
@end
.m
#import "RootView.h"
@implementation RootView
//重写 初始化方法
-(instancetype)initWithFrame:(CGRect)frame
{
if (self= [super initWithFrame:frame]) {
[self addAllViews];
}
return self;
}
//添加视图
-(void)addAllViews
{
#pragma mark ========= UISegmentedControl ===============
// UISegmentedControl : UIControl
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:@[@"移动",@"联通",@"电信"]];
segmentedControl.frame = CGRectMake(20, 30, 300, 30);
self.segmentedControl = segmentedControl;
[self addSubview:segmentedControl];
#pragma mark ======== segmentedControl 控制的图片 ================
UIImageView *yidong = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"yidong.png"]];
yidong.frame = CGRectMake(20, 80, 300, 200);
self.yidong = yidong;
[self addSubview:yidong];
UIImageView *dianxin = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"dianxin.png"]];
dianxin.frame = CGRectMake(20, 80, 300, 200);
[self addSubview:dianxin];
self.dianxin = dianxin;
UIImageView *liantong = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"liantong.png"]];
liantong.frame = CGRectMake(20, 80, 300, 200);
self.liantong = liantong;
[self addSubview:liantong];
#pragma mark ============ UISlider ==============================
UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(20, 380, 350, 30)];
//
slider.minimumValueImage = [UIImage imageNamed:@"minSound.png"];
slider.maximumValueImage = [UIImage imageNamed:@"maxSound.png"];
self.slider = slider;
[self addSubview:slider];
}
@end
//动画效果图:直接添加视图
UIImageView *imageViews = [[UIImageView alloc]init];
imageViews.frame = CGRectMake(10, 50, 200, 300);
NSMutableArray *arrayImage = [NSMutableArray array];
for (int i =0; i<12; i++) {
NSString *name = [NSString stringWithFormat:@"image_%d.tiff",i];
UIImage *image = [UIImage imageNamed:name];
[arrayImage addObject:image];
}
imageViews.animationImages = arrayImage;
self.imageViews = imageViews;
[self addSubview:imageViews];
imageViews.animationDuration = 2;
imageViews.animationRepeatCount = 5;
[imageViews startAnimating];