另一種選擇是(例如具有6 UIButtons
):
創建的按鈕。 e.g在viewDidLoad
:
//This is an array with your buttons positions
self.cgPointsArray = @[[NSValue valueWithCGPoint:CGPointMake(25, 130)],[NSValue valueWithCGPoint: CGPointMake(70, 130)],
[NSValue valueWithCGPoint:CGPointMake(115, 130)],[NSValue valueWithCGPoint:CGPointMake(160, 130)],
[NSValue valueWithCGPoint:CGPointMake(205, 130)],[NSValue valueWithCGPoint:CGPointMake(250, 130)]];
// for loop to allocate the buttons
for (int i = 0; i < [self.cgPointsArray count]; i++)
{
NSValue *value = [self.cgPointsArray objectAtIndex:i];
CGPoint point = [value CGPointValue];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(point.x, point.y, 20, 20);
[button setBackgroundColor:[UIColor blueColor]];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[self.view addSubview:button];
}
然後你就可以通過管理在tags
事件:
- (void) buttonClicked: (id) sender
{
UIButton *tempButton = (UIButton*) sender;
int tag = tempButton.tag;
if (tag == 0)
{
//Do something
}
else if (tag == 1)
{
//Do something
}
else if (tag == 2)
{
//Do something
}
else if (tag == 3)
{
//Do something
}
else if (tag == 4)
{
//Do something
}
else if (tag == 5)
{
//Do something
}
}
你爲什麼需要這些網點?你如何使用它們? – rdelmar
我希望我的回答可以幫助你 –