如何从UIScrollView获取UIButton位置

时间:2015-04-23 10:09:10

标签: ios objective-c uiscrollview uibutton

我有一个scrollView,上面有很多按钮。我试图从滚动中获取按钮位置,以便将其滚动到该位置。我的问题是,一旦点击每个按钮,我总是在_btnV.frame.origin.x中得到相同的值。那么请问我如何解决这个问题呢?

- (void)scrollView{

    segmentWidth = screenWidth / 5;

    NSArray *images = @[@"Smiley-1.png",@"Smiley-2.png",@"Smiley-3.png",@"Smiley-4.png",@"Smiley-5.png",@"Smiley-5.png",@"Smiley-5.png",@"Smiley-4.png",@"Smiley-1.png",@"Smiley-2.png",@"Smiley-3.png",@"Smiley-4.png"];

    long ScrollContentSize = ((images.count -1)*segmentWidth)+screenWidth;

    _scrlView.contentSize = CGSizeMake(ScrollContentSize, _scrlView.frame.size.height);

    float screenX2 = screenHeight/2 - segmentWidth/2;

    __block float screenX = (screenWidth-segmentWidth)/2;

    NSMutableArray *arr = [[NSMutableArray alloc] init];

    for(int i=0; i<images.count; i++)
    {
        _btnV = [[UIButton alloc]init];
        [_btnV setFrame:CGRectMake(screenX, screenX2, segmentWidth, segmentWidth)];
        [_btnV setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",[images objectAtIndex:i]]] forState:UIControlStateNormal];
        [_btnV setBackgroundColor:[UIColor redColor]];
        [_btnV addTarget:self action:@selector(scrollToMe:) forControlEvents:UIControlEventTouchUpInside];

        [_btnV setTag:i];

        [arr addObject:_btnV];
        [_scrlView addSubview:_btnV];

        screenX = screenX+segmentWidth;
    }

    _menuItems = arr;

    [self.view addSubview:_scrlView];
}


-(void) scrollToMe:(id)sender{

    int scrollX = _scrlView.contentOffset.x;
    int mod = scrollX % segmentWidth;
    int position = scrollX/segmentWidth;
    float positionOffset = (float) mod / segmentWidth;

    scrollX = position * segmentWidth;

    NSLog(@"position:%f",_btnV.frame.origin.x);

    //scrollX = ...

    [_scrlView setContentOffset:CGPointMake(scrollX, scrollX) animated:YES];
}

3 个答案:

答案 0 :(得分:2)

使用其标记值

找到该按钮
-(void) scrollTo:(id)sender{

    UIButton * button = (UIButton *)sender;
    int position = button.tag;

    scrollX = position * segmentWidth;

    NSLog(@"position:%f",_btnV.frame.origin.x);

    //scrollX = ...

    [_scrlView setContentOffset:CGPointMake(scrollX, scrollX) animated:YES];
}

答案 1 :(得分:1)

您正在调用UIButton的选择器方法。所以在这种方法中,你可以直接获得相对于UIScrollView的UIButton位置。像

-(void) scrollTo:(id)sender{

    UIButton* btnClick = (UIButton*)sender;
    NSLog(@"X position : %f",btnClick.frame.origin.x);
}

希望这对你有所帮助。

答案 2 :(得分:0)

试试这个,它会正确记录你所有按钮的位置

 -(void) scrollTo:(id)sender{

    int scrollX = _scrlView.contentOffset.x;
    int mod = scrollX % segmentWidth;
    int position = scrollX/segmentWidth;
    float positionOffset = (float) mod / segmentWidth;

    scrollX = position * segmentWidth;
for(int i=0; i<images.count; i++)
{   if([[[_scrlView subviews] objectAtIndex:i] isKindOfClass:[UIButton class]]) {
        Button btn = [[_scrlView subviews] objectAtIndex:i];
NSLog(@"position:%f",btn.frame.origin.x);
    }

}
    //scrollX = ...

    [_scrlView setContentOffset:CGPointMake(scrollX, scrollX) animated:YES];
}