0

ページあたり 75x75 で単一のボタンを表示する 100x100 ピクセルのスクロール ビュー (ページング モード) を作成しようとしています。最初の画像を表示できますが、次のページに移動できません。これが私が使用しているコードです、誰かが私を助けてくれますか?

.h

@property (nonatomic , retain) IBOutlet UIScrollView *scrollMenu;

.m

@synthesize scrollMenu;

-(void)viewDidLoad {

scrollMenu.pagingEnabled = YES;
NSInteger numberOfButtons = 2;

for (int i = 0; i < numberOfButtons; i++) {

    //Array of images for the buttons
    NSArray *menuItems = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"1.png"], [UIImage imageNamed:@"2.png"], nil];

    //Create A Button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    //Give the button an action
    [button addTarget:self action:@selector(menuItemSelected:) forControlEvents:UIControlEventTouchUpInside];

    //Give the button an image
    [button setImage:[menuItems objectAtIndex:i] forState:UIControlStateNormal];

    //Most likely WRONG
    button.frame = CGRectMake(i*(20+75), 8.0, 75, 75);


    button.showsTouchWhenHighlighted=YES;

    //Assign a tag
    button.tag = i;

    //Add the button to the view
    [scrollMenu addSubview:button];

}
//Most likely WRONG
scrollMenu.contentSize = CGSizeMake(100,100);
[self.view addSubview:scrollMenu];
 }

  [super viewDidLoad];
}
4

1 に答える 1

0

同様の種類のものを実装しましたが、UIImageView 用です。ここでは、UI ScrollView のいくつかのプロパティを設定し、デリゲート メソッドを 1 つ実装する必要があるだけです。

最初にいくつかのプロパティを設定します

- (void)viewDidLoad
{
    [super viewDidLoad];

    _scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    _scrollView.multipleTouchEnabled=YES;
    _scrollView.scrollEnabled=YES;
    _scrollView.directionalLockEnabled=YES;
    _scrollView.canCancelContentTouches=YES;
    _scrollView.delaysContentTouches=YES;
    _scrollView.clipsToBounds=YES;
    _scrollView.alwaysBounceHorizontal=YES;
    _scrollView.bounces=YES;
    _scrollView.pagingEnabled=YES;
    _scrollView.showsVerticalScrollIndicator=NO;
    _scrollView.showsHorizontalScrollIndicator=NO;
    _scrollView.delegate=self;

}

デリゲート メソッドを実装する

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    @try
    {
        CGFloat pageWidth = 320;    //scrollView.frame.size.width;
        int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;

        if (page >9)
            page = page - 3;
        if (page <[listOfPictures count])
        {
            [_scrollView setContentOffset:CGPointMake(320*page, _scrollView.contentOffset.y) animated:YES];
            currentPicIndex = page;
        }

    }
    @catch (NSException *exception)
    {
        TRACE_ERROR(@"scrollViewDidEndDecelerating", exception.name, exception.description);
    }

}

私のスクロールビューは240X300です。したがって、ボタン、スクロールビューのピクセルで操作し、ContentOffSet を賢明に設定してください。

これがあなたを助けることを願っています。

于 2013-02-02T06:27:58.990 に答える