2016-12-21 2 views
3

私はプログラム的にいくつかのボタンを作成しようとします。私はそのボタンが画像1のようにサークルの周りに作成されます。今私はいくつかの結果を持って、私はボタンを回転することができますが、それらの間のオフセットを設定する方法を知らない。今私はこの結果があります:画像2ここに私のコードです。 そして何か助けてくれてありがとう!いくつかの半径を持つ円の周りにボタンを作成する方法

のViewControllerでの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]; 

} 

私はそれぞれの新しいボタンの作成ポイント

- (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である必要がありますいくつかのx、yのポイントを生成しよう私は自分のボタンを作成する

-(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

あなたはGENIUSです!ありがとうございました! –

+0

@DmitriyBabinskiyちょうどアンカーポイントを変更する – Leo

関連する問題