iOS-UIView 视图、简单的动画

2018-03-21  本文已影响14人  我是谁重要吗

每一个控件都是一个容器 所以一个控件能放到另一个控件的内部

未标题-1.png
//方法1:添加一个view
    UIView* view = [[UIView alloc] init];
    //距离父视图左边距和上边距的距离,本身自己的宽和高
    CGRect rect = CGRectMake(10, 30, 100, 100);
    //确定当前视图的位置与大小
    view.frame = rect;
    //改变背景颜色
    view.backgroundColor = [UIColor redColor];
    //可以把我们的view添加到window上
    [self.window addSubview:view];
    [view release];
    
    //方法2:再添加一个view
    UIView* view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
    view2.backgroundColor = [UIColor greenColor];
    [view addSubview:view2];
    [view2 release];
    */
    
    ////循环创建 5 个view 么么哒
    for (int i = 0; i < 5; i++) {
        //随机颜色
        int red = arc4random()%255;
        int green = arc4random()%255;
        int blue = arc4random()%255;
        //NSLog(@"%d %d %d ",red,green,blue);
        //设置颜色哟
        UIColor* color = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];
        
        //10,60,110,160,210
        float x = 10 + 50 * i;
        //30,80,130,180,230
        float y = 30 + 50 * i;
        UIView* view = [[UIView alloc] initWithFrame:CGRectMake(x, y, 100, 100)];
        view.backgroundColor = color;
        [self.window addSubview:view];
        ///[view release];
    }
    
    //父视图上所有的子视图
    //self.window.subviews
    //子视图拿到父视图指针
    //view1.superview
    //移除视图
    //[view1 removeFromSuperview];
    //视图隐藏
    //view1.hidden = YES;
    //透明度
    //view2.alpha = 0.5;
    
    //放到哪个视图之上
    //[self.window insertSubview:view2 aboveSubview:view3];
    //放到哪个视图之下
    //[self.window insertSubview:view3 belowSubview:view1];
    //[self.window insertSubview:view1 atIndex:2];
    
    //把子视图放到最上面
    //[self.window bringSubviewToFront:view1];
    //把子视图放到最下面
    //[self.window sendSubviewToBack:view3];
    //交换子视图的位置
    //[self.window exchangeSubviewAtIndex:0 withSubviewAtIndex:2];
    
    
    
    
    //让子视图自适应父视图的改变
    bgView.autoresizesSubviews = YES;
    //设置子视图的适应方式
    subView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;

这里介绍一下 UIView的简单的动画效果

//动画块
    [UIView beginAnimations:nil context:nil];
    //时间
    [UIView setAnimationDuration:10.0];
    //动画效果
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:NO];
    //减速效果
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationWillStartSelector:@selector(animationWillStart)];
    [UIView setAnimationDidStopSelector:@selector(animationWillStop)];
    
    ////下面是自己要做的事情 这里交换一下两个UIView的位置
    [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    
    [UIView commitAnimations];
- (void)animationWillStart{
    NSLog(@"动画 开始");
}

- (void)animationWillStop{
    NSLog(@"动画结束");
}
////////////////渐变消失
        UIImageView *splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-100)];
        splashView.image = [UIImage imageNamed:@"222"];
        NSURL *url = [NSURL URLWithString:str];
        [splashView setImageWithURL:url];
        [self.window addSubview:splashView];
        [self.window bringSubviewToFront:splashView];
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:3.0];
        [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:
         self.window cache:YES];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(startupAnimationDone)];
        splashView.alpha = 0.0;
        splashView.frame = CGRectMake(-60, -85, 440, 635);
        [UIView commitAnimations];

旋转

CGAffineTransform myaffine3 = CGAffineTransformMakeRotation(3.1415026/4);
    view1.transform = myaffine3;//断点2

旋转渐变消失

[UIView animateWithDuration:3.0 animations:^{
        view1.frame = CGRectMake(self.view.bounds.size.width-50, 100, 50, 50);
        view1.alpha = 0;
        view1.transform = CGAffineTransformMakeRotation(3.1415926);
    } completion:^(BOOL finished) {
        NSLog(@"animation completion!");
    }];

推荐

ios核心动画高级技巧

上一篇下一篇

猜你喜欢

热点阅读