每個控件都是一個容器 因此一個控件能放到另外一個控件的內部ios
屬性git
@property(nonatomic) CGRect frame; @property(nonatomic) CGRect bounds; @property(nonatomic) CGPoint center;
方法dom
- (void)removeFromSuperview; - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index; - (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2; - (void)addSubview:(UIView *)view; - (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview; - (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview; - (void)bringSubviewToFront:(UIView *)view; - (void)sendSubviewToBack:(UIView *)view;
示例:動畫
//方法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的簡單的動畫效果 atom
//動畫塊 [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(@"動畫結束"); }
漸變消失url
////////////////漸變消失 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
旋轉漸變消失code
[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!"); }];
推薦orm
ios核心動畫高級技巧rem