Ios 以编程方式在UIScrollView中依次单击10个UIButtons
我正在开发一个控件,其中在UIScrollView中添加了10个UIButton。现在,我有一个要求,点击每个按钮后,一个接一个的一些延迟。你们能告诉我怎么做吗 下面是我所做的代码 在viewcontroller.h文件中Ios 以编程方式在UIScrollView中依次单击10个UIButtons,ios,objective-c,for-loop,uiscrollview,uibutton,Ios,Objective C,For Loop,Uiscrollview,Uibutton,我正在开发一个控件,其中在UIScrollView中添加了10个UIButton。现在,我有一个要求,点击每个按钮后,一个接一个的一些延迟。你们能告诉我怎么做吗 下面是我所做的代码 在viewcontroller.h文件中 @property (weak) IBOutlet UIScrollView *mapScrollView; @property (strong) UIButton *addContactIcon; 在viewcontroller.m文件中 // Set up Scroll
@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中的每个UIButtons调用Action方法。因此,我在下面做了一些错事。请帮助我:
for(NSUInteger count=0; count<[_distanceList count];count++) {
[UIView animateWithDuration:0.4
delay:0.8
options:0
animations:^{
[_addContactIcon sendActionsForControlEvents:UIControlEventTouchUpInside];
} completion:nil];
}
for(NSUInteger count=0;countHi)您可以执行如下操作来单击视图上的所有UI按钮
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);
}
ui按钮*按钮;
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
UIButton*buttonMaster;
buttonMaster=[UIButton Button类型为:UIButtonTypeRoundRect];
[按钮主添加目标:self
操作:@选择器(主按钮标记:)
forControlEvents:UIControlEventTouchUpInside];
[按钮主设置标题:@“显示视图”状态:uicontrol状态正常];
buttonMaster.frame=CGRectMake(80.0,y,160.0,40.0);
buttonMaster.tag=100;
[self.view addSubview:buttonMaster];
y=60;
对于(int x=0;x为什么不直接调用mapsScrollButtonClicked 10次?@evgeny:效果不好。你也可以检查一下。