gpt4 book ai didi

ios - 如何围绕具有一定半径的圆创建按钮

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:18:42 26 4
gpt4 key购买 nike

我尝试以编程方式创建一些按钮。我会像图 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

最佳答案

截图

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,然后应用不同的变换。

关于ios - 如何围绕具有一定半径的圆创建按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41262470/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com