Programaticaly在UIScrollView中依次单击10个UIButtons

时间:2015-06-29 13:13:32

标签: ios objective-c for-loop uiscrollview uibutton

我正在开发一个控件,其中在UIScrollView中添加了10个UIButtons。现在我需要在延迟一个接一个地点击每个按钮。你能指导我怎么做吗?

这是我所做的代码。

在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调用Action Method。因为我在下面做错了。帮助我:

for(NSUInteger count=0; count<[_distanceList count];count++)    {

            [UIView animateWithDuration:0.4
                              delay:0.8
                            options:0
                         animations:^{
                             [_addContactIcon sendActionsForControlEvents:UIControlEventTouchUpInside];
                         } completion:nil];
 }

1 个答案:

答案 0 :(得分:0)

您好,您可以执行以下操作,点击视图中的所有UIButtons。

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);


}