在ScrollView上设置按钮图像时出现问题

时间:2013-10-14 17:51:03

标签: iphone ios objective-c uiscrollview

我正在制作一款应用。我的应用程序基于此示例项目。 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];
} 
凯尔

2 个答案:

答案 0 :(得分:0)

要在正常状态下设置按钮图像,传递 forState:的参数是 UIControlStateNormal 而不是 UIControlState突出显示

答案 1 :(得分: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];
}