UIVIew实战


前一篇博客介绍了UIView原理知识,在本篇博客中将重点介绍UIVIew应用方面的知识。

1.创建一个view

CGRect viewRect = CGRectMake(10,10,100,100);
UIView *myView = [[UIView alloc] initWithFrame:viewRect];
DEMO:
  1. #import "ZSViewController.h"  
  2. #define BVWIDTH 300  
  3. #define BVHEIGHT 300  
  4. #define MVWIDTH 200  
  5. #define MVHEIGHT 200  
  6. #define TVWIDTH 100  
  7. #define TVHEIGHT 100  
  8. @interface ZSViewController ()  
  9.   
  10. @end  
  11.   
  12. @implementation ZSViewController  
  13.   
  14. - (void)viewDidLoad  
  15. {  
  16.     [super viewDidLoad];  
  17.     UIView *buttomView = [self createViewWithRect:CGRectMake(self.view.center.x-BVWIDTH/2self.view.center.y-BVHEIGHT/2, BVWIDTH, BVHEIGHT)];  
  18.     [buttomView setBackgroundColor:[UIColor redColor]];  
  19.     [self.view addSubview:buttomView];  
  20.     UIView *topView = [self createViewWithRect:CGRectMake(self.view.center.x-TVWIDTH/2self.view.center.y-TVHEIGHT/2, TVWIDTH, TVHEIGHT)];  
  21.     [topView setBackgroundColor:[UIColor greenColor]];  
  22.     [self.view addSubview:topView];  
  23.     UIView *midView = [self createViewWithRect:CGRectMake(self.view.center.x-MVWIDTH/2self.view.center.y-MVHEIGHT/2, MVWIDTH, MVHEIGHT)];  
  24.     [midView setBackgroundColor:[UIColor yellowColor]];  
  25.     [self.view insertSubview:midView aboveSubview:buttomView];  
  26.       
  27. }  
  28.   
  29. /** 
  30.  * create a new view with rect 
  31.  */  
  32. - (UIView *)createViewWithRect:(CGRect)rect{  
  33.     UIView *view = [[UIView alloc] initWithFrame:rect];  
  34.     return view;  
  35. }  
  36.   
  37. - (void)didReceiveMemoryWarning  
  38. {  
  39.     [super didReceiveMemoryWarning];  
  40.     // Dispose of any resources that can be recreated.  
  41. }  
  42.   
  43. @end  
效果图:

drawRect Demo:
  1. #import "ZSCircleView.h"  
  2.   
  3. @implementation ZSCircleView  
  4.   
  5. - (void)drawRect:(CGRect)rect{  
  6.     CGContextRef context = UIGraphicsGetCurrentContext();  
  7.     [self drawCircleWithPoint:CGPointMake(arc4random()%300, arc4random()%500) context:context];  
  8.     [self drawCircleWithPoint:CGPointMake(arc4random()%300, arc4random()%500) context:context];  
  9.     [self drawCircleWithPoint:CGPointMake(arc4random()%300, arc4random()%500) context:context];  
  10.     [self drawCircleWithPoint:CGPointMake(arc4random()%300, arc4random()%500) context:context];  
  11.     [self drawCircleWithPoint:CGPointMake(arc4random()%300, arc4random()%500) context:context];  
  12.     [self drawCircleWithPoint:CGPointMake(arc4random()%300, arc4random()%500) context:context];  
  13.     [self drawCircleWithPoint:CGPointMake(arc4random()%300, arc4random()%500) context:context];  
  14.     [self drawCircleWithPoint:CGPointMake(arc4random()%300, arc4random()%500) context:context];  
  15. }  
  16.   
  17. - (void)drawCircleWithPoint:(CGPoint)pos context:(CGContextRef)context{  
  18.     UIGraphicsPushContext(context);  
  19.     CGContextSetLineWidth(context, 10);  
  20.     [[UIColor colorWithRed:arc4random()%255/100 green:arc4random()%255/100 blue:arc4random()%255/100 alpha:1] setStroke];  
  21.     CGContextBeginPath(context);  
  22.     CGContextAddArc(context, pos.x, pos.y, arc4random()%100+1002*M_PI, YES);  
  23.     CGContextStrokePath(context);  
  24.     UIGraphicsPopContext();  
  25. }  
  26. @end  
效果图:

objective-c 中三种产生随机数的方法

  1. //arc4random() 比较精确不需要生成随即种子  
  2. //通过arc4random() 获取0到x-1之间的整数的代码如下:  
  3.     int value = arc4random() % x;   
  4. //获取1到x之间的整数的代码如下:  
  5.     int value = (arc4random() % x) + 1;  
  6.     
  7. //CCRANDOM_0_1() cocos2d中使用 ,范围是[0,1]  
  8.     float random = CCRANDOM_0_1() * 5//[0,5]   CCRANDOM_0_1() 取值范围是[0,1]  
  9.     
  10. //random() 需要初始化时设置种子  
  11.     srandom((unsigned int)time(time_ttime_t *)NULL); //初始化时,设置下种子就好了。  
注意:要做一个手绘板的话不能直接在drawRect里操作,应当使用类UIBezierPath

综合DEMO:
  1. #import "RichRandomSmallBall.h"  
  2. #define RANDOMBALLX 150  
  3. #define RANDOMBALLY 100  
  4.   
  5. @interface RichRandomSmallBall(){  
  6.     CGAffineTransform first ;  
  7.     CGAffineTransform second ;  
  8.     CGAffineTransform third ;  
  9. }  
  10. @property (nonatomic,strong)UIImage *img;  
  11.   
  12.   
  13. @end  
  14.   
  15. @implementation RichRandomSmallBall  
  16.   
  17.   
  18. - (id)initWithFrame:(CGRect)frame  
  19. {  
  20.     self = [super initWithFrame:frame];  
  21.     if (self) {  
  22.           
  23.     }  
  24.     return self;  
  25. }  
  26.   
  27. - (void)awakeFromNib{  
  28.     [super awakeFromNib];  
  29.     self.backgroundColor = [UIColor greenColor];  
  30.     self.clipsToBounds = YES;  
  31.     [self setup];  
  32.   
  33. }  
  34.   
  35. - (void)setup{  
  36.     self.img = [UIImage imageNamed:@"circle.png"];  
  37.     self.first_ball = [[UIImageView alloc] initWithImage:self.img];  
  38.     self.second_ball = [[UIImageView alloc] initWithImage:self.img];  
  39.     self.third_ball = [[UIImageView alloc] initWithImage:self.img];  
  40.     [self addSubview:self.first_ball];  
  41.     [self addSubview:self.second_ball];  
  42.     [self addSubview:self.third_ball];  
  43.     [self initRandomBall];  
  44.     [self setRandomBallPosition];  
  45.     [self randomBallAnimate];  
  46. }  
  47.   
  48.   
  49. - (void)initRandomBall{  
  50.     self.first_ball.frame = CGRectMake(self.center.x-RANDOMBALLX/2, -RANDOMBALLY/2, RANDOMBALLX   , RANDOMBALLY);  
  51.     self.second_ball.frame = CGRectMake(-RANDOMBALLX/2self.frame.size.height-RANDOMBALLY/2, RANDOMBALLX, RANDOMBALLY);  
  52.     self.third_ball.frame = CGRectMake(self.frame.size.width-RANDOMBALLX/2self.frame.size.height-RANDOMBALLY/2, RANDOMBALLX, RANDOMBALLY);  
  53.     self.first_ball.alpha = 0.3;  
  54.     self.second_ball.alpha = 0.3;  
  55.     self.third_ball.alpha = 0.3;  
  56.       
  57. }  
  58.   
  59. - (void) setRandomBallPosition{  
  60.     first = self.first_ball.transform;  
  61.     second = self.second_ball.transform;  
  62.     third = self.third_ball.transform;  
  63. }  
  64.   
  65. - (void)getRandomBallPosition{  
  66.     self.first_ball.transform = first;  
  67.     self.second_ball.transform = second;  
  68.     self.third_ball.transform = third;  
  69.     self.first_ball.alpha = 0.3;  
  70.     self.second_ball.alpha = 0.3;  
  71.     self.third_ball.alpha = 0.3;  
  72. }  
  73.   
  74. - (void) randomBallAnimate{  
  75.     [self getRandomBallPosition];  
  76.     [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{  
  77.         float x = self.center.x ;  
  78.         float y = self.center.y ;  
  79.         self.first_ball.transform = CGAffineTransformScale(CGAffineTransformTranslate(self.first_ball.transform,0 ,y), 1.51.5);  
  80.         self.second_ball.transform = CGAffineTransformScale(CGAffineTransformTranslate(self.second_ball.transform,x+RANDOMBALLX/2,-y), 1.51.5);  
  81.         self.third_ball.transform = CGAffineTransformScale(CGAffineTransformTranslate(self.third_ball.transform,-x-RANDOMBALLX/2 ,-y), 1.51.5);  
  82.         self.first_ball.alpha = 1;  
  83.         self.second_ball.alpha = 1;  
  84.         self.third_ball.alpha = 1;  
  85.     } completion:^(BOOL finished){  
  86.           
  87.     }];  
  88. }  
  89.   
  90. /* 
  91. // Only override drawRect: if you perform custom drawing. 
  92. // An empty implementation adversely affects performance during animation. 
  93. - (void)drawRect:(CGRect)rect 
  94. { 
  95.     // Drawing code 
  96. } 
  97. */  
  98.   
  99. @end  
  100.   
  101. #import "RichViewController.h"  
  102. #define RANDOMBALLX 150  
  103. #define RANDOMBALLY 100  
  104.   
  105. @interface RichViewController ()  
  106.   
  107. @end  
  108.   
  109. @implementation RichViewController  
  110.   
  111. - (IBAction)randomForMore:(UIButton *)sender {  
  112.     [self.randomSmallBallView randomBallAnimate];  
  113. }  
  114.   
  115.   
  116. - (void)viewDidLoad  
  117. {  
  118.     [super viewDidLoad];  
  119.     self.randomSmallBallView.first_ball.userInteractionEnabled = YES;  
  120.     self.randomSmallBallView.second_ball.userInteractionEnabled = YES;  
  121.     self.randomSmallBallView.third_ball.userInteractionEnabled = YES;  
  122.     [self.randomSmallBallView.first_ball addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(firstRandomBallTaped)]];  
  123.     [self.randomSmallBallView.second_ball addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(secondRandomBallTaped)]];  
  124.     [self.randomSmallBallView.third_ball addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(thirdRandomBallTaped)]];  
  125.       
  126.     UIImage *img = [UIImage imageNamed:@"bubble.png"];  
  127. //    [img resizableImageWithCapInsets:UIEdgeInsetsMake(50, 50, 50, 50)];  
  128. //    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];  
  129. //    UIImageView *imgView = [[UIImageView alloc] initWithImage:[img stretchableImageWithLeftCapWidth:21 topCapHeight:14]];  
  130.     UIImageView *imgView = [[UIImageView alloc] initWithImage:[img resizableImageWithCapInsets:UIEdgeInsetsMake(15.52215.522)]];  
  131.     imgView.frame = CGRectMake(0,400 , img.size.width+100, img.size.height+100);  
  132.     UILabel *label = [[UILabel alloc] init];  
  133.     label.text = @"hello world";  
  134.     label.frame = CGRectMake(200100100);  
  135.     label.adjustsFontSizeToFitWidth = YES;  
  136.       
  137.     [imgView addSubview:label];  
  138.     [self.view addSubview:imgView];  
  139.   
  140.       
  141. }  
  142.   
  143. - (void)firstRandomBallTaped{  
  144.     [self performSegueWithIdentifier:@"randomBallTaped" sender:self];  
  145. }  
  146.   
  147. - (void)secondRandomBallTaped{  
  148.     [self performSegueWithIdentifier:@"randomBallTaped" sender:self];  
  149. }  
  150.   
  151. - (void)thirdRandomBallTaped{  
  152.     [self performSegueWithIdentifier:@"randomBallTaped" sender:self];  
  153. }  
  154.   
  155. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{  
  156.     if([segue.identifier isEqualToString:@"randomBallTaped"]){  
  157.           
  158.     }  
  159. }  
  160.   
  161. - (void)didReceiveMemoryWarning  
  162. {  
  163.     [super didReceiveMemoryWarning];  
  164.     // Dispose of any resources that can be recreated.  
  165. }  
  166.   
  167.   
  168.   
  169. @end  











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值