UIButton 的图像使用滚动视图滚动,但它的动作/方法保持在原始位置
Posted
技术标签:
【中文标题】UIButton 的图像使用滚动视图滚动,但它的动作/方法保持在原始位置【英文标题】:UIButton's image scrolling with scrollview but its action/method remains on original position 【发布时间】:2014-05-30 05:21:19 【问题描述】:我有一个自动滚动的 UIScrollView,我使用 initWithFrame 创建了几个按钮,每个按钮都有一个图像和一个独特的操作(方法),并将它们全部添加到滚动视图中。按钮的图像可以使用滚动视图自动滚动,但是当我单击每个按钮时,这不是我分配给该按钮的方法。当我点击同一个位置时,无论经过哪个按钮,它都会转到相同的方法而不是那个按钮的方法。下面是我创建按钮的代码:
for (int i = 0; i < 4; i++)
//create an imageView object in every 'page' of the scrollView.
CGRect frame;
frame.origin.x = 62 * i;
frame.origin.y = 0;
frame.size = CGSizeMake(52.0, 52.0);
myButton = [[UIButton alloc] initWithFrame:frame];
[myButton setImage:[MyImageArray objectAtIndex:i] forState:normal];
switch (i)
case 0:
[myButton addTarget:self action:@selector(aMethod0) forControlEvents:UIControlEventTouchUpInside];
break;
case 1:
[myButton addTarget:self action:@selector(aMethod1) forControlEvents:UIControlEventTouchUpInside];
break;
case 2:
[myButton addTarget:self action:@selector(aMethod2) forControlEvents:UIControlEventTouchUpInside];
break;
case 3:
[myButton addTarget:self action:@selector(aMethod3) forControlEvents:UIControlEventTouchUpInside];
break;
default:
break;
[self.MyScrollView addSubview:myButton];
下面是自动滚动scrollview的代码:
- (void)move_pagination
UIPageControl *pgCtr = MyPageControl;
CGFloat contentOffset = MyScrollView.contentOffset.x;
__block int nextPage = (int)(contentOffset/(MyScrollView.frame.size.width+10)) + 1 ;
[UIView animateWithDuration:10
delay:0
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^
if( nextPage!=3 )
MyScrollView.contentOffset = CGPointMake(nextPage*(10+MyScrollView.frame.size.width), 0);
pgCtr.currentPage=nextPage;
// else start sliding from 1 :)
else
nextPage = 1;
MyScrollView.contentOffset = CGPointMake(0, 0);
MyScrollView.contentOffset = CGPointMake(nextPage*(10+MyScrollView.frame.size.width), 0);
pgCtr.currentPage=2;
completion:nil];
任何线索为什么会发生这种奇怪的行为?谢谢。
【问题讨论】:
@remus,它与 62 相乘而不是加法,所以它将是 0、62、124、186。所以这不是问题 【参考方案1】:试试这个代码..
在你看来确实加载方法
float x=62;
for(int i=0;i<4;i++)
self.stack=[UIButton buttonWithType:UIButtonTypeCustom];
self.stack.frame=CGRectMake(x+(62*i), 100, 52, 52);
[self.stack setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.stack setTitle:[self.titleName objectAtIndex:i] forState:UIControlStateNormal];
[self.stack setBackgroundColor:[UIColor redColor]];
self.stack.tag=i;
[self.stack addTarget:self action:@selector(actionMethod:) forControlEvents:UIControlEventTouchUpInside];
//your scroll view
//[self.scrView addSubview:self.btnlabe1];
[self.view addSubview:self.stack];
并创建这样的方法...
-(void)actionMethod:(id)sender
UIButton *button=(UIButton *)sender;
switch (button.tag)
case 0:
NSLog(@"ACTIONMETHOD CODE HERE");
break;
case 1:
NSLog(@"ACTIONMETHOD1 CODE HERE");
break;
case 2:
NSLog(@"ACTIONMETHOD2 CODE HERE");
break;
case 3:
NSLog(@"ACTIONMETHOD3 CODE HERE");
break;
default:
break;
修改后的代码
float x=62;
for(int i=0;i<4;i++)
self.stack=[UIButton buttonWithType:UIButtonTypeCustom];
self.stack.frame=CGRectMake(x+(62*i), 100, 52, 52);
[self.stack setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.stack setTitle:[temp objectAtIndex:i] forState:UIControlStateNormal];
[self.stack setBackgroundColor:[UIColor redColor]];
switch (i)
case 0:
[self.stack addTarget:self action:@selector(aMethod0) forControlEvents:UIControlEventTouchUpInside];
break;
case 1:
[self.stack addTarget:self action:@selector(aMethod1) forControlEvents:UIControlEventTouchUpInside];
break;
case 2:
[self.stack addTarget:self action:@selector(aMethod2) forControlEvents:UIControlEventTouchUpInside];
break;
case 3:
[self.stack addTarget:self action:@selector(aMethod3) forControlEvents:UIControlEventTouchUpInside];
break;
default:
break;
//your scroll view
//[self.scrView addSubview:self.stack];
[self.view addSubview:self.stack];
和方法
-(void)aMethod0
NSLog(@"action 0");
-(void)aMethod1
NSLog(@"action 1");
-(void)aMethod2
NSLog(@"action 2");
-(void)aMethod3
NSLog(@"action 3");
【讨论】:
没有错误,和我原来的方法一样 我认为这不是分配按钮的好方法..更改按钮框架 嗨@karthikeyan,你能说得更具体点吗,我需要在按钮框架等上更改什么。谢谢 是的,如上所述,我的问题的行为相同.. @karthikeyan 放置断点并检查方法是否正在调用?它的工作原理以上是关于UIButton 的图像使用滚动视图滚动,但它的动作/方法保持在原始位置的主要内容,如果未能解决你的问题,请参考以下文章