Believe in your own future, will thank yourself right now Sinner Yun

Sinner_Yun

image

 

 

UIView升级&UIImageView

 

 

【UIView属性及方法】

 

//@property(nonatomic) CGRect frame;

//@property(nonatomic) CGRect bounds;

//@property(nonatomic,readonly) UIView *superview;

//@property(nonatomic,readonly,copy) NSArray *subviews;

 

//- (void)addSubview:(UIView *)view;

//- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;

//- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;

//- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;

 

//- (void)removeFromSuperview;

 

//2- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;

//- (void)bringSubviewToFront:(UIView *)view;

//- (void)sendSubviewToBack:(UIView *)view;

 

//yes- (BOOL)isDescendantOfView:(UIView *)view;

通过找- (UIView *)viewWithTag:(NSInteger)tag;

 

 

【跟随/停靠模式】

//允许子视图跟随,默认就是yes(这个是给父视图设置的)

@property(nonatomic) BOOL autoresizesSubviews;

//随父视图变化的效果(这个是给子视图设置的)

@property(nonatomic) UIViewAutoresizing autoresizingMask;    

 

UIView【基础动画效果】,三种实现方式

【一】

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:0.5];

//do something

[UIView commitAnimations];

 

【二】

[UIView animateWithDuration:0.5 animations:^{

    //do something

    }];

 

【三】

[UIView animateWithDuration:0.5 animations:^{

    //do something

    } completion:^(BOOL finished){

        //动画完成后执行

    }];

 

 

【UIImageView】图片视图类

 

除了用allco initWithFrame的方式创建,还可以用图片直接初始化,如果用图片直接初始化,记得设frame

 

//图片

@property(nonatomic,retain) UIImage *image;

 

//动画的图片数组

@property(nonatomic,copy) NSArray *animationImages;

 

//动画时间(也就是一次完整的动画需要的时间)

@property(nonatomic) NSTimeInterval animationDuration;

 

//动画循环次数,循环完以后会自动停止

@property(nonatomic) NSInteger animationRepeatCount; 

 

//开始动画

- (void)startAnimating;

 

//手动结束动画

- (void)stopAnimating;

 

//判断动画状态

- (BOOL)isAnimating;

 

 

 

 

 

 

 

 

 

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    UIView *bigView = [[UIView alloc] initWithFrame:CGRectMake(10, 30, 300, 300)];

    bigView.backgroundColor = [UIColor redColor];

    [self.view addSubview:bigView];

    

    NSArray *colorArr = [NSArray arrayWithObjects:[UIColor orangeColor], [UIColor yellowColor], [UIColor greenColor], nil];

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

        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i*30+10, i*30+10, 120, 120)];

        view.backgroundColor = [colorArr objectAtIndex:i];

        [bigView addSubview:view];

    }

    

    //查询某一个view所有的子view

    NSArray *subArr = [bigView subviews];

    

    //通过序号查询某一个子view,声明一个指针指向他

    UIView *midView = [subArr objectAtIndex:1];

    

    midView.backgroundColor = [UIColor blackColor];

    

#pragma mark - 插入

    

    UIView *insertView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 120, 120)];

    insertView.backgroundColor = [UIColor cyanColor];

    

    //在固定的层级插入一个视图(如果越界,结果和addSubView一样)

//    [bigView insertSubview:insertView atIndex:100];

    

    //在某一个子view的上面插入

//    [bigView insertSubview:insertView aboveSubview:midView];

    

    //在某一个子视图的下面插入

    [bigView insertSubview:insertView belowSubview:midView];

    

    

#pragma mark - 删除

    

    //自杀

    [insertView removeFromSuperview];

    

    

#pragma mark - 改

    

    //通过层级找到对应的2个视图,然后交换这2个视图的层级

//    [bigView exchangeSubviewAtIndex:1 withSubviewAtIndex:2];

    

    //将一个子视图放到最下面

    [bigView sendSubviewToBack:midView];

    

    //将一个子视图带到最上面

    [bigView bringSubviewToFront:midView];

    

    //延迟调用

    [self performSelector:@selector(qqq:) withObject:midView afterDelay:3];

}

 

- (void)qqq:(UIView *)view

{

    //找到某个view的父视图

    UIView *sView = [view superview];

    

    [sView removeFromSuperview];

}

 

 

 

 

 

 

 

 

 

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    UIView *bigView = [[UIView alloc] initWithFrame:CGRectMake(10, 30, 100, 150)];

    bigView.backgroundColor = [UIColor redColor];

    bigView.tag = 1;

    [self.view addSubview:bigView];

    

    UIView *smallView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];

    smallView.backgroundColor = [UIColor blueColor];

    [bigView addSubview:smallView];

    

    //允许子视图跟随

    bigView.autoresizesSubviews = YES;

    

    //设置子视图的跟随模式

    smallView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;

    

    

    //获取屏幕高度

    CGFloat height = [UIScreen mainScreen].bounds.size.height;

    

    //这种格式的btn自带大小和图片

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeInfoLight];

    

    //自带大小,所以直接设置中心点坐标就可以

    btn.center = CGPointMake(160, height-20-11);

    

    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    //将一个rect转化成字符串

    NSLog(@"%@",NSStringFromCGRect(btn.frame));

    

    

    UIButton *bBtn = [UIButton buttonWithType:UIButtonTypeContactAdd];

    bBtn.center = CGPointMake(200, height-20-11);

    [bBtn addTarget:self action:@selector(bBtnClick) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:bBtn];

}

 

- (void)bBtnClick

{

    UIView *bigView = [self.view viewWithTag:1];

    

    [UIView animateWithDuration:2 animations:^{

        bigView.alpha = 0;

    } completion:^(BOOL finished) {

        NSLog(@"动画结束以后回调");

        bigView.alpha = 1;

    }];

    

    NSLog(@"透明度动画开始");

}

 

- (void)btnClick:(UIButton *)sender

{

    sender.selected = !sender.selected;

    

    UIView *bigView = [self.view viewWithTag:1];

    CGRect frame = bigView.frame;

    

    if (sender.selected) {

        frame.size.width *= 2;

        frame.size.height *= 2;

        

        //开启简单动画

        [UIView beginAnimations:nil context:NULL];

        

        //设置动画时间

        [UIView setAnimationDuration:2];

        

        bigView.frame = frame;

        

        bigView.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0f green:arc4random_uniform(256)/255.0f blue:arc4random_uniform(256)/255.0f alpha:1];

        

        //提交动画

        [UIView commitAnimations];

        

    } else {

        frame.size.width /= 2;

        frame.size.height /= 2;

        

        //实现动画的简写方式

        [UIView animateWithDuration:2 animations:^{

            bigView.frame = frame;

            bigView.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0f green:arc4random_uniform(256)/255.0f blue:arc4random_uniform(256)/255.0f alpha:1];

        }];

    }

    

    NSLog(@"改变大小");

}

 

 

 

 

 

 

 

 

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor grayColor];

    

    //创建一个图片视图

    UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(30, 60, 150, 300)];

    

    //设置需要展示的图片

    iv.image = [UIImage imageNamed:@"aa"];

    //如果运行这个程序设备支持Retina,会优先查找aa@2x.png

    //如果找不到,再去查找aa.png

    //如果设备不支持Retina,直接查找aa.png

    

    iv.backgroundColor = [UIColor redColor];

    [self.view addSubview:iv];

    

    //使用一个图片初始化图片视图

    UIImageView *aiv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bb"]];

    

    //设置frame,宽和高通过图片获取

    aiv.frame = CGRectMake(180, 100, aiv.image.size.width, aiv.image.size.height);

    

    [self.view addSubview:aiv];

    

    NSLog(@"%@",NSStringFromCGRect(aiv.frame));

}

 

 

 

 

 

 

 

 

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor = [UIColor yellowColor];

    

    UIImageView *iv = [[UIImageView alloc] initWithFrame:self.view.bounds];

    iv.image = [UIImage imageNamed:@"cat_angry0000.jpg"];

    [self.view addSubview:iv];

    

    NSMutableArray *imageArr = [NSMutableArray array];

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

        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"cat_angry%04d.jpg",i]];

        [imageArr addObject:image];

    }

    

    //设置iv的动画数组、单次循环的时间、循环次数

    iv.animationImages = imageArr;

    iv.animationDuration = imageArr.count/10;

    iv.animationRepeatCount = 3;

    

    iv.tag = 1;

    

    //允许用户交互(可以接收点击事件),iv默认是不接收的

    iv.userInteractionEnabled = YES;

    

    UIButton *btn = [[UIButton alloc] initWithFrame:self.view.bounds];

    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];

    [iv addSubview:btn];

}

 

- (void)btnClick

{

    UIImageView *iv = (UIImageView *)[self.view viewWithTag:1];

    

    if ([iv isAnimating]) {

        //如果iv正在播放动画,那么让他就停止

        [iv stopAnimating];

        return;

    }

    

    //开始动画

    [iv startAnimating];

}

 

 

 

 

 

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    

    NSArray *numArr=[NSArray arrayWithObjects:@"1",@"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12", @"13",@"14",  nil];

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

        

            UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0, i*30+30, 30, 30)];

            [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

            [btn setTitle:[numArr objectAtIndex:i] forState:UIControlStateNormal];

            btn.titleLabel.font=[UIFont boldSystemFontOfSize:20];

            [self.view addSubview:btn];

        

    }

  

    

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

 

            UIView *lView=[[UIView alloc]initWithFrame:CGRectMake(30, i*30+30, arc4random_uniform(290), 20)];

            lView.backgroundColor=[UIColor blueColor];

            [self.view addSubview:lView];

            lView.tag=i+1;

        

        

    }

 

    CGFloat heigth=[UIScreen mainScreen].bounds.size.height;

    CGFloat width=[UIScreen mainScreen].bounds.size.width;

    UIButton *btn1=[UIButton buttonWithType:UIButtonTypeSystem];

    btn1.frame=CGRectMake(width/2-20, heigth-20-50, 40, 20);

    [btn1 setTitle:@"NEXT" forState:UIControlStateNormal];

    [btn1 addTarget:self action:@selector(aClick) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn1];

}

 

 

-(void)aClick

{

    NSArray *arr = [self.view subviews];

    for (int i=0; i<[arr count]; i++) {

        

        UIView *subview = [arr objectAtIndex:i];

        if (subview.tag>0&&subview.tag<15) {

            CGRect frame = subview.frame;

            frame.size.width = arc4random_uniform(290);

            subview.backgroundColor= [UIColor colorWithRed:arc4random_uniform(256)/255.0f green:arc4random_uniform(256)/255.0f blue:arc4random_uniform(256)/255.0f alpha:1];

            

            [UIView beginAnimations:nil context:NULL];

            [UIView setAnimationDuration:0.5];

            

            subview.frame = frame;

            

            [UIView commitAnimations];

        }

    }

 

 

}

 

 

posted on 2014-03-18 20:30  Sinner_Yun  阅读(235)  评论(0编辑  收藏  举报

导航