如何在半径范围内创建圆形按钮

时间:2016-12-21 12:06:12

标签: ios objective-c uibutton cgaffinetransform

我尝试以编程方式创建多个按钮。我在圆圈周围创建的按钮就像在图像1上一样。现在我有一些结果,我可以旋转按钮,但不知道如何设置它们之间的偏移。现在我有了这个结果:image 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];

}

我尝试生成一些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

1 个答案:

答案 0 :(得分:5)

截图

enter image description here

代码:

anchorPoint

关键点为anchorPoint,您将所有按钮设置为相同frameCalendar cal_alarm; ,然后应用不同的转换。