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

Dmi*_*kiy 3 objective-c uibutton cgaffinetransform ios

我尝试以编程方式创建一些按钮.我在圈子周围创建的按钮就像在图像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];

}
Run Code Online (Sandbox Code Playgroud)

我尝试生成一些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;
}
Run Code Online (Sandbox Code Playgroud)

这是我的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;
}
Run Code Online (Sandbox Code Playgroud)

图1:

图片1

图2:

图片2

Leo*_*Leo 5

截图

在此输入图像描述

和代码:

- (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];
    }
}
Run Code Online (Sandbox Code Playgroud)

关键的一点是anchorPoint,你将所有的按钮,同anchorPointframe,然后应用不同的变换.