【问题标题】:Trouble Setting Button Image When On a ScrollView在 ScrollView 上时无法设置按钮图像
【发布时间】:2013-10-14 17:51:03
【问题描述】:

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

凯尔

【问题讨论】:

    标签: iphone ios objective-c uiscrollview


    【解决方案1】:

    为了将按钮图像设置为正常状态,要传递的参数 forState:UIControlStateNormal 而不是 UIControlStateHighlighted

    【讨论】:

      【解决方案2】:

      因此,在 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];
      }
      

      【讨论】:

      • 对不起,我的意思是 UIControlStateNormal。错字。我的按钮在头文件中声明。该按钮是滚动视图的子视图。当有人点击按钮时,它会替换图像。然后用另一个图像替换它。就像由于某种原因无法引用它一样。哦,我应该提到这是一个循环。其中 i = 0 且 i
      • 您需要发布更多代码。您的问题中没有足够的信息来诊断您的问题。
      • 好的。重新输入了问题。感谢您提供的所有帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多