加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – UIScrollViewDelegate没有开火

发布时间:2020-12-14 18:55:59 所属栏目:百科 来源:网络整理
导读:这可能是一些 sort of duplicate of this question.但我试图应用它的答案无济于事. 我有一个带有pagingEnabled的UIScrollView.我想使用UIScrollViewDelegate来帮助检测我所在的页面.这是我的代码: @interface SearchMissionViewController : UIViewControll
这可能是一些 sort of duplicate of this question.但我试图应用它的答案无济于事.

我有一个带有pagingEnabled的UIScrollView.我想使用UIScrollViewDelegate来帮助检测我所在的页面.这是我的代码:

@interface SearchMissionViewController : UIViewController <UIScrollViewDelegate> {

    UIScrollView *scroll;

}
    @property (nonatomic,retain) UIScrollView *scroll;

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView;
    - (IBAction) popView;


    @end




- (void)viewDidLoad
{
    [super viewDidLoad] ;

    // Here I have also tried scroll.delegate = self;
    self.scroll.delegate = self;


    self.view.backgroundColor = [UIColor redColor];
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,self.view.frame.size.width,self.view.frame.size.height)];

    // Is this correct? I've added it to try to get the UIScrollView Delegate calling scrollViewDidEndScrollingAnimation (or whatever)
    // [scroll setContentOffset:CGPointMake(0.0,0.0) animated:YES];


    scroll.pagingEnabled = YES;
    NSInteger numberOfViews = 3;
    for (int i = 0; i < numberOfViews; i++) {
        CGFloat yOrigin = i * self.view.frame.size.width;
        UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin,self.view.frame.size.height)];
        awesomeView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];


        UIImageView *targetView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"targetImageA.png"]];
        targetView.frame = CGRectMake(0.0,0.0,139,153);
        targetView.frame = CGRectMake(yOrigin+90,80,153);


        UILabel *targetNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(yOrigin+95,230,100,20)];
        targetNameLabel.text = @"Bond";
        targetNameLabel.backgroundColor = [UIColor clearColor];
        targetNameLabel.textColor = [UIColor redColor];


        UILabel *missionNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(yOrigin+95,250,20)];
        missionNameLabel.text = @"Mission";
        missionNameLabel.backgroundColor = [UIColor clearColor];
        missionNameLabel.textColor = [UIColor redColor];



        [scroll addSubview:awesomeView];        
        [scroll addSubview:targetView];
        [scroll addSubview:targetNameLabel]; 
        [scroll addSubview:missionNameLabel];

        // Why does this get released? Wouldn't it then dissappear from the scroll view? Should I not be releasing other objects from above?
        [awesomeView release];


    }
    scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews,self.view.frame.size.height);
    [self.view addSubview:scroll];
    [scroll release]; 




    // Overlap image
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,320,460)];
    imgView.image= [UIImage imageNamed:@"mission-viewfinder.png"];
    [self.view addSubview:imgView];


    // Place back button ontop of the image. 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom ];
    [button addTarget:self 
               action:@selector(popView)
    forControlEvents:UIControlEventTouchDown];
    button.frame = CGRectMake(0.0,100.0,60.0);
    [self.view addSubview:button];


    // Do any additional setup after loading the view from its nib.
}








- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@" ");
    NSLog(@" Scroll View Did End Decelerating");
    NSLog(@" ");  

}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@" ");
    NSLog(@" Scroll View Did End Scroll ! ! ! ");
    NSLog(@" ");  

}

本质上问题是上面的两个方法,scrollViewDidEndDeclerating和scrollViewDidScroll都没有打印.换句话说,他们没有被召唤.

有任何想法吗?

解决方法

问题在于陈述的顺序:

self.scroll.delegate = self;
...
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height)];

您尝试在创建scrollview之前设置委托,因此该语句无效,因为scroll为nil.改为:

self.scroll = [[[UIScrollView alloc] initWithFrame:CGRectMake(...)] autorelease];
self.scroll.delegate = self;

我刚注意到你的评论:

// Why does this get released? Wouldn't it then dissappear from the scroll view? Should I not be releasing other objects from above?
[awesomeView release];

是的,您应该在将它们添加到scrollView后释放targetView,targetNameLabel和missionNameLabel.您拥有它们是因为您创建了它们(通过alloc-init),您必须通过向它们发送释放消息来放弃它们的所有权.当您将它们添加到scrollView时,它将保留它们,如addSubview文档中所述:以确保它们在scrollView需要时不会被释放.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读