我正在开发一个控件,其中在UIScrollView中添加了10个UIButton。现在,我需要在延迟一段时间后一个接一个地单击每个按钮。你们都可以指导我怎么做吗?
这是我所做的代码。
在viewcontroller.h文件中]
@property (weak) IBOutlet UIScrollView *mapScrollView; @property (strong) UIButton *addContactIcon;
在viewcontroller.m文件中]
// Set up ScrollView with UIButtons NSUInteger xPosition = 1; NSUInteger countIndex = 0; for (ContactModule *sortedContacts in _distanceList) { _addContactIcon = [[UIButton alloc] initWithFrame:CGRectMake(xPosition, 7, 30, 30)]; _addContactIcon.tag = countIndex; [_addContactIcon addTarget:self action:@selector(mapsScrollButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; _addContactIcon.layer.cornerRadius = 2.0; _addContactIcon.clipsToBounds = YES; [_addContactIcon setBackgroundImage:[UIImage imageWithContentsOfFile:dataPath] forState:UIControlStateNormal]; [_addContactIcon setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [_mapScrollView addSubview:_addContactIcon]; xPosition += _addContactIcon.frame.size.width + 2; countIndex = countIndex + 1; } _mapScrollView.contentSize = CGSizeMake(30 * _distanceList.count + 34, 40); [_mapScrollView setShowsHorizontalScrollIndicator:NO];
这里是每个按钮的按钮单击方法:
- (void)mapsScrollButtonClicked:(UIButton *)clickButton { // Set Annotation Callout [_mapsView selectAnnotation:[_mapsView.annotations objectAtIndex:clickButton.tag] animated:YES]; }
现在要求是我要为UIScrollview中的每个UIButton调用操作方法。为此,我在下面做错了。帮助我:
for(NSUInteger count=0; count<[_distanceList count];count++) { [UIView animateWithDuration:0.4 delay:0.8 options:0 animations:^{ [_addContactIcon sendActionsForControlEvents:UIControlEventTouchUpInside]; } completion:nil]; }
我正在开发一个控件,其中在UIScrollView中添加了10个UIButton。现在,我需要在延迟一段时间后一个接一个地单击每个按钮。你们都可以指导我怎么做吗?这里...
[您好,您可以像下面这样进行操作,以单击视图上的所有UIButton。
UIButton *button;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *buttonMaster;
buttonMaster = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonMaster addTarget:self
action:@selector(masterButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
[buttonMaster setTitle:@"Show View" forState:UIControlStateNormal];
buttonMaster.frame = CGRectMake(80.0, y, 160.0, 40.0);
buttonMaster.tag = 100;
[self.view addSubview:buttonMaster];
y = 60;
for (int x=0; x<=10; x++)
{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(button1:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, y, 160.0, 30.0);
button.tag = x;
//[paintView addSubview:button];
[self.view addSubview:button];
y = y + 70;
}
[buttonMaster sendActionsForControlEvents:UIControlEventTouchUpInside];
if ([[self.view viewWithTag:0] isKindOfClass:[UIButton class]]) {
//UIButton *button = (UIButton *) view;
// do something funky with button
UIButton *myBtns = (UIButton *)[self.view viewWithTag:0];
NSLog(@" button tapped is %ld", myBtns.tag);
[myBtns sendActionsForControlEvents:UIControlEventTouchUpInside];
[myBtns setBackgroundColor:[UIColor greenColor]];
} else {
// do something funky with view
NSLog(@"do nothing ");
}
}
- (void)listSubviewsOfView:(UIView *)view {
// Get the subviews of the view
NSArray *subviews = [view subviews];
// Return if there are no subviews
//if ([subviews count] == 0) return; // COUNT CHECK LINE
for (UIView *subview in subviews) {
UIButton *myBtns = (UIButton *)[self.view viewWithTag:subview.tag];
//UIButton *myBtns = (UIButton *)[self.view viewWithTag:0];
if(myBtns.tag == 100 ){
NSLog(@"Master Button tag :- Dont do anything ");
}else
{
//[myBtns sendActionsForControlEvents:UIControlEventTouchUpInside];
NSLog(@"tags are %ld ", myBtns.tag);
}
// List the subviews of subview
//[self listSubviewsOfView:subview];
}
}
-(void) clickAllButtons{
for (int i = 0; i<=10; i++) {
UIButton *myBtns = (UIButton *)[self.view viewWithTag:i];
[myBtns sendActionsForControlEvents:UIControlEventTouchUpInside];
}
}
- (IBAction)masterButtonTapped:(UIButton*)sender{
//[self listSubviewsOfView:self.view];
// [self clickAllButtons];
}
- (IBAction)button1:(UIButton*)sender{
NSLog(@"test");
//NSLog(@"Button clicked %ld", tag);
}