在 UIScrollView 中以编程方式依次单击 10 个 UIButton
Posted
技术标签:
【中文标题】在 UIScrollView 中以编程方式依次单击 10 个 UIButton【英文标题】:Programmatically Click 10 UIButtons one after another in UIScrollView 【发布时间】:2015-06-29 13:13:31 【问题描述】:我正在开发一个控件,其中在 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 中的每个 UIButtons 调用 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];
【问题讨论】:
为什么不直接调用 mapsScrollButtonClicked 10 次? @Evgeniy:效果不好。你也可以检查一下。 【参考方案1】:您好,您可以执行以下操作来单击视图上的所有 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);
【讨论】:
以上是关于在 UIScrollView 中以编程方式依次单击 10 个 UIButton的主要内容,如果未能解决你的问题,请参考以下文章
在 UIStackView 中以编程方式添加内容时的 UIScrollView 高度
在 Swift 中以编程方式添加时,UIScrollView 根本不滚动?