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:
圖2:
你是天才!非常感謝您的幫助! –
@DmitriyBabinskiy只是改變anchorPoint – Leo