0

我正在制作一个应用程序。我的应用程序基于此示例项目。https://github.com/yeahdongcn/RSCircaPageControl

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

在 SV 上,我随后添加了一个名为 UIButton *button 的按钮。当我在“两分钟”后尝试更改按钮的图像时,什么也没有发生。这是因为有 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];
} 

凯尔

4

2 回答 2

0

用于将按钮图像设置为正常状态传递的参数forState: is UIControlStateNormal not UIControlStateHighlighted

于 2013-10-14T18:09:30.113 回答
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];
}
于 2013-10-14T18:09:43.727 回答