2013-10-14 34 views
0

我正在制作应用程序。我的应用程序基于此示例项目。 https://github.com/yeahdongcn/RSCircaPageControl在滚动查看时设置按钮图像的故障

在应用程序中有一个视图。在视图中,有一个滚动视图(UIScrollView * scrollView),它允许10个“页面”的内容。在每个页面中,都有另一个滚动视图(UIScrollView * sv),它是* scrollView的子视图。

在SV上,我添加了一个名为UIButton *按钮的按钮。当我在“两分钟”之后尝试更改按钮的图像时,没有任何反应。这是因为有10个按钮。我只需要更改页面上的按钮图像一个的页面。不是所有的人。

东西告诉我它需要“索引处的对象”或其他东西来指定哪个按钮在哪个页面上我想改变。

代码如下!多谢你们!

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

self.clipView = [[RSView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)]; 
self.clipView.clipsToBounds = YES; 
[self.view addSubview:self.clipView]; 

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.clipView.bounds.size.width, kScrollViewHeight)]; 
self.scrollView.delegate = self; 
self.scrollView.clipsToBounds = NO; 
self.scrollView.pagingEnabled = YES; 
self.scrollView.showsHorizontalScrollIndicator = NO; 
self.scrollView.showsVerticalScrollIndicator = NO; 
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
[self.view addSubview:self.scrollView]; 
self.clipView.scrollView = self.scrollView; 

self.pageControl = [[RSCircaPageControl alloc] initWithNumberOfPages:10]; 
CGRect frame = self.pageControl.frame; 
frame.origin.x = self.view.bounds.size.width - frame.size.width - 10; 
frame.origin.y = roundf((self.view.bounds.size.height - frame.size.height)/2.); 
self.pageControl.frame = frame; 
self.pageControl.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin; 
[self.pageControl setCurrentPage:0 usingScroller:NO]; 
[self.view addSubview:self.pageControl]; 

CGFloat currentY = 0; 
for (int i = 0; i < 10; i++) { 
    UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, currentY, self.scrollView.bounds.size.width, kScrollViewHeight)]; 
    sv.tag = kScrollViewTagBase + i; 
    sv.delegate = self; 
    sv.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
    sv.backgroundColor = [UIColor colorWithRed:(arc4random() % 257)/256. green:(arc4random() % 257)/256. blue:(arc4random() % 257)/256. alpha:1]; 



    ///here's the buttton!! 


    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    UIImage*normal = [UIImage imageNamed:@"girl.jpg"]; 
    UIImage*selected = [UIImage imageNamed:@"girl2.jpg"]; 
    button.frame = CGRectMake(100, 100, 50,50)); 
    [button setImage:normal forState:UIControlStateNormal]; 
    [button setImage:selected forState:UIControlStateHighlighted]; 
    button.contentMode = UIViewContentModeScaleAspectFit; 
    [button addTarget:self 
       action:@selector(myButtonPressed:) 
    forControlEvents:UIControlEventTouchDown]; 
    [sv addSubview:button]; //notice that the button is added as a subview of SV 

    [self.scrollView addSubview:sv]; // notice that SV is a subview of the scrollView. 


    if (i == 2 || i == 6) { 
     sv.contentSize = CGSizeMake(sv.contentSize.width, kScrollViewContentHeight); 
    } 



    currentY += kScrollViewHeight; 
} 

self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, currentY); 

    [self performSelector:changeButtomImages withObject:nil afterDelay:TwoMinutes]; 

} 

- (void)changeButtonImages { 

    UIImage*normal2 = [UIImage imageNamed:@"boy.jpg"]; 
    UIImage*selected2 = [UIImage imageNamed:@"boy2.jpg"]; 
    [button setImage:normal2 forState:UIControlStateNormal]; 
    [button setImage:selected2 forState:UIControlStateHighlighted]; 
} 

凯尔

回答

0

在正常状态下的参数设置按钮图像通过forState:UIControlStateNormal没有UIControlStateHighlighted

0

所以,在changeButtonImages要更改的图像对于iVar“按钮”,但你实际上从来没有将任何事情分配给该按钮var。在viewDidLoad的for循环中,您正在创建一个名为button的UIButton局部变量。也许这样?

- (void)myButtonPressed:(id)sender 
{ 
    UIButton *button = (UIButton *)sender; 
    UIImage*normal2 = [UIImage imageNamed:@"boy.jpg"]; 
    UIImage*selected2 = [UIImage imageNamed:@"boy2.jpg"]; 
    [button setImage:normal2 forState:UIControlStateNormal]; 
    [button setImage:selected2 forState:UIControlStateHighlighted]; 
} 
+0

对不起,我的意思是UIControlStateNormal。错字。我的按钮在头文件中声明。该按钮是滚动视图的子视图。当有人点击按钮时,它会替换图像。然后用另一张图片替换它。这只是因为某些原因无法引用它。 哦,我应该提到,这是一个循环。我i = 0,我i <10与i ++。所以实际上有十个按钮在带分页的滚动视图中创建。 如果有帮助。有人建议对它进行分类。不知道这是否有帮助。有任何想法吗。也许objectAtIndex或什么?谢谢。 –

+0

您需要发布更多的代码。您的问题中没有足够的信息来诊断您的问题。 – JonahGabriel

+0

好的。给我一下我会编辑这个问题。 –