0

我的源代码下载

- - - 更新 - - - -

我正在尝试使用CGAffineTransformMakeTranslate模拟滑入和滑出来实现侧边栏效果。我想将我的侧边栏设置为滚动视图,以便添加更多数据,但根本无法滚动。

这是我的代码:

SidebarView是一个UITableView

#import "SidebarView.h"

@interface SidebarView ()
@property (nonatomic, readwrite) CGFloat offsetX;
@end

@implementation SidebarView

#pragma mark - Initilization

- (void)setup {
    // do initilization here
    self.offsetX = self.frame.size.width;
    [self registerClass:[UITableViewCell class] forCellReuseIdentifier:@"sidebarCell"];
}

- (void)awakeFromNib {
    [self setup];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    [self setup];
    return self;
}

#pragma mark - Custom function

- (void)show {
    self.transform = CGAffineTransformMakeTranslation(-self.offsetX, 0);
}

- (void)hide {
    self.transform = CGAffineTransformMakeTranslation(-self.offsetX, 0);
}

我的视图控制器:

#import "ViewController.h"

#import "SidebarView.h"


@interface ViewController () <UITableViewDataSource>
@property (nonatomic) BOOL isMenuHide;
@property (nonatomic, strong) SidebarView *sidebarView;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.isMenuHide = YES;
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

    if (self.sidebarView) {
        self.sidebarView = nil;
    }
    if (self.view) {
        self.view = nil;
    }

}

- (SidebarView *)sidebarView {
    if (!_sidebarView) {
        CGRect frame = [[UIScreen mainScreen] bounds];
        frame.size.width /= 2;
        _sidebarView = [[SidebarView alloc] initWithFrame:frame];
        _sidebarView.transform = CGAffineTransformMakeTranslation(-_sidebarView.offsetX, 0);
        _sidebarView.contentSize = CGSizeMake(320, 960);
        _sidebarView.scrollEnabled = YES;
        _sidebarView.showsVerticalScrollIndicator = YES;
        _sidebarView.dataSource = self;
    }
    return _sidebarView;
}

#define ANIMATE_DURATION 0.5

- (IBAction)showMenu:(UIBarButtonItem *)sender {
    if (self.isMenuHide) {
        [self.view addSubview:self.sidebarView];

        [UIView animateWithDuration:ANIMATE_DURATION animations:^{
            [self.sidebarView show];
            self.view.transform = CGAffineTransformMakeTranslation(self.sidebarView.offsetX, 0);
        }];
    } else {
        [UIView animateWithDuration:ANIMATE_DURATION animations:^{
            [self.sidebarView hide];
            self.view.transform = CGAffineTransformMakeTranslation(0, 0);
        } completion:^(BOOL finished) {
            [self.sidebarView removeFromSuperview];
        }];
    }
    self.isMenuHide = !self.isMenuHide;
}

#pragma mark - UITableView Datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 11;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sidebarCell"];
    cell.textLabel.text = @"Bingo";
    return cell;
}

@end

请告诉我“为什么我的滚动视图无法滚动”谢谢。

4

1 回答 1

1

contentSize 需要设置为所包含内容的大小,而不是它所在的框架的大小。

我不知道您的代码是否还有其他问题,但这是最终端的问题。

于 2013-09-15T06:56:58.750 回答