做图形界面开发的感觉是,可以运用到一些数学知识了,哈哈。需求是将若干小图片平均分布在圆的周长上。效果如下:

imageimage

用右侧图来表示,就是要求x和y的坐标。iPad的坐标系从左上开始。

圆参数方程公式:

x=R*cos(A)

y=R*sin(A)

 

这个公式的坐标系是按照圆心来的,如果按照屏幕的坐标系,还需要加上偏移量。

具体到本例中,任意多个图,比如n个,就相当于对2PI个弧度取n分之一。

实现代码:

- (void)viewDidLoad { 
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide]; 
    self.view=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
    self.view.backgroundColor=[UIColor blueColor];

    NSArray *imageNames=[[NSArray alloc] initWithObjects:@"4.png",@"5.png",@"6.png",@"7.png",@"8.png",@"9.png",
                         @"10.png",@"11.png",@"12.png",@"13.png",@"14.png",@"15.png", 
                         nil]; 
    
    UIView *contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
    [self.view addSubview:contentView]; 
    
    int r=180; 
    
    for (int i=0; i<[imageNames count]; i++) { 
        UIImage *image=[UIImage imageNamed:[imageNames objectAtIndex:i]]; 
        int x=1024/2+r*cos(M_PI*2.0/[imageNames count]*i)-image.size.width/2; 
        int y=768/2+r*sin(M_PI*2.0/[imageNames count]*i)-image.size.height/2; 
        
        CGRect  viewRect = CGRectMake(x, y, image.size.width, image.size.height); 
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:viewRect]; 
        [imageView setImage:image]; 
        [contentView addSubview:imageView];

        [imageView release]; 
        [image release]; 
    } 
    
    NSLog(@">>>,%@",contentView.subviews); 
    
    [contentView release]; 
    [imageNames release]; 
}

posted on 2011-02-19 16:28  David@_@  阅读(329)  评论(0编辑  收藏  举报