2016-12-21 84 views
3

我嘗試以編程方式創建多個按鈕。我在圖像1上圍繞圓圈創建了哪些按鈕。現在我有了一些結果,我可以旋轉按鈕,但不知道如何設置它們之間的偏移量。現在我有這個結果:image 2這是我的代碼。 並感謝您的幫助!如何創建具有某個半徑的圓周圍的按鈕

viewDidLoad中的視圖控制器

CGPoint point = self.view.center; 

NSArray *arrayWithImages = @[@"red", @"pink", @"green", @"blue", @"purple", @"orange", @"yellow"]; 
NSArray *arrayWithAngle = @[@(0), @(M_PI/3.8f), @(M_PI/1.8), @(M_PI/-6), @(M_PI/6), @(M_PI/-1.8), @(M_PI/-3.8)]; 

NSMutableArray *arrayWithPlacePoint = [self generatePointForRound:point andAngle:arrayWithAngle andRadius:25.0f]; 
NSLog(@"array with place point = %@", arrayWithPlacePoint); 

for (NSInteger i = 0; i < [arrayWithImages count]; i++) { 

    PetalObject *petal = [[PetalObject alloc] initWithImageName:arrayWithImages andRotation:arrayWithAngle andIndex:i andPointsArray:arrayWithPlacePoint andCentrPoint:point]; 
    [self.view addSubview:petal.petalButton]; 

} 

我嘗試生成一些X,Y點,這應該是每一個新的按鈕的創作心態

- (NSMutableArray *)generatePointForRound:(CGPoint)centrPoint andAngle:(NSArray *)arrayAngle andRadius:(float)radiusValue { 

CGPoint currentPoint; 
NSMutableArray *array = [[NSMutableArray alloc] init]; 

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

    CGFloat x1 = centrPoint.x + (radiusValue * sin([[arrayAngle objectAtIndex:i] doubleValue])); 
    CGFloat y1 = centrPoint.y + (radiusValue * cos([[arrayAngle objectAtIndex:i] doubleValue])); 

    currentPoint = CGPointMake(x1, y1); 
    [array addObject:[NSValue valueWithCGPoint:currentPoint]]; 

} 

return array; 
} 

這裏是我的PetalObject其中我創建我的按鈕

-(id)initWithImageName:(NSArray *)imageNameArray andRotation:(NSArray *)angleArray andIndex:(NSInteger)index andPointsArray:(NSMutableArray *)pointsArray andCentrPoint:(CGPoint)centerPoint { 

    self.isRecorded = NO; 
self.isComplited = NO; 

UIImage *image = [UIImage imageNamed:imageNameArray[index]]; 
CGPoint point = [[pointsArray objectAtIndex:index] CGPointValue]; 

self.petalButton = [[UIButton alloc] initWithFrame:CGRectMake(point.x - 43 , point.y - 180, 86, 168)]; 

//self.petalButton.transform = CGAffineTransformMake(cos(angle),sin(angle),-sin(angle),cos(angle),boundsPoint.x-boundsPoint.x*cos(angle)+boundsPoint.y*sin(angle),boundsPoint.y-boundsPoint.x*sin(angle)-boundsPoint.y*cos(angle)); 

[self.petalButton setBackgroundImage:image forState:UIControlStateNormal]; 
CGFloat angle = [[angleArray objectAtIndex:index] floatValue]; 
[self.petalButton setTransform:CGAffineTransformMakeRotation(angle)]; 

    return self; 
} 

圖1:

image 1

圖2:

image 2

回答

5

截圖

enter image description here

和代碼:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    for (NSInteger index = 0; index < 6; index ++) { 
     UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 20)]; 
     button.layer.anchorPoint = CGPointMake(-0.5, 0.5); 
     button.backgroundColor = [UIColor greenColor]; 
     button.center = CGPointMake(self.view.center.x + CGRectGetWidth(button.frame)/2.0, self.view.center.y); 
     button.transform = CGAffineTransformMakeRotation(M_PI * 2 * index/6.0); 
     [self.view addSubview:button]; 
    } 
} 

關鍵點是anchorPoint,您將所有按鈕設置爲相同的anchorPointframe,然後應用不同的轉換。

+0

你是天才!非常感謝您的幫助! –

+0

@DmitriyBabinskiy只是改變anchorPoint – Leo