【问题标题】:UILabel set framework doesn't work if the value is negative如果值为负,UILabel 设置框架不起作用
【发布时间】:2015-03-10 16:24:58
【问题描述】:

我已经下载了这个项目,它展示了一个关于分页的小例子:

Paging example project

我添加了一个标签,并通过使用 scrollViewDidScroll: 委托方法,该方法在发生任何滚动操作时调用,我调用了一个方法来重置我添加的标签的 x 位置。如果我让 x 位置在我收到它时,位置变化会按预期发生,但是如果我将接收到的参数值恢复为负值,它就不起作用了。

代码如下: .h 文件:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIScrollViewDelegate>

- (void)pageChanged ;

@end

和 .m 文件: #import "ViewController.h"

@interface ViewController (){

    UIPageControl *pageControl;
    UIScrollView *scroll;
    UILabel *myLabel;
    int curentLabelX;
    int initialLabelX;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor clearColor];

    // Scroll View

    scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
    scroll.backgroundColor=[UIColor clearColor];
    scroll.delegate=self;
    scroll.pagingEnabled=YES;
    [scroll setContentSize:CGSizeMake(scroll.frame.size.width*3, scroll.frame.size.height)];

  // page control
    pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 424, 320, 36)];
    pageControl.backgroundColor=[UIColor blackColor];
    pageControl.numberOfPages=3;
    [pageControl addTarget:self action:@selector(pageChanged) forControlEvents:UIControlEventValueChanged];
    pageControl.alpha = 0;
    CGFloat x=0;
    for(int i=1;i<4;i++)
    {
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(x+0, 0, 320, 460)];
        [image setImage:[UIImage imageNamed:[NSString stringWithFormat:@"image%d.jpg",i]]];
        [scroll addSubview:image];

        x+=320;
    }
    [self.view addSubview:scroll];

        [self.view addSubview:pageControl];
    initialLabelX = 100;

    myLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    myLabel.backgroundColor = [UIColor redColor];
    [self.view addSubview:myLabel];

}

- (void)scrollViewDidScroll:(UIScrollView *)_scrollView{

//    NSLog(@"%@", _scrollView);
    CGFloat viewWidth = _scrollView.frame.size.width;
    // content offset - tells by how much the scroll view has scrolled.

    int pageNumber = floor((_scrollView.contentOffset.x - viewWidth/50) / viewWidth) +1;

    pageControl.currentPage=pageNumber;

    int myLabelContentOffset = _scrollView.contentOffset.x;
    NSLog(@"Scroll offset: %d", myLabelContentOffset);
    [self changeLabelOffset:myLabelContentOffset];
}

- (void)changeLabelOffset:(int) byScrollView{
    curentLabelX = byScrollView;
    curentLabelX = -curentLabelX;
    NSLog(@"Label X pos: %d", initialLabelX + curentLabelX);
    [myLabel setFrame:CGRectMake(initialLabelX + curentLabelX, 100, 100, 100)];

}

- (void)pageChanged {
    int pageNumber = (int)pageControl.currentPage;
    CGRect frame = scroll.frame;
    frame.origin.x = frame.size.width*pageNumber;
    frame.origin.y=0;
    [scroll scrollRectToVisible:frame animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {

    [super viewDidUnload];
}

@end

【问题讨论】:

    标签: ios objective-c uiscrollview uilabel frame


    【解决方案1】:

    问题是标签会随着滚动同时改变位置和速度,所以它看起来是正常的,因为它根本不动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-18
      • 1970-01-01
      • 1970-01-01
      • 2013-10-31
      • 1970-01-01
      相关资源
      最近更新 更多