将静态 UIButton 叠加到 UICollectionView 上
Overlay a static UIButton onto a UICollectionView
我有一个 UICollectionView
,它有一个 UICollectionViewCell
的数组,每个单元格占据视图的 80%。
我想在每次按下时滚动到下一个单元格的屏幕上添加一个静态 UIButton
,我必须将按钮 subview
添加到 Parent 视图而不是 UICollectionView
因为它是静态的。
我的问题是:如何将叠加层添加到主视图上以及如何通过按下按钮以编程方式滚动视图?
我在哪里实施 collection 视图
@interface EvaluationViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UIBarButtonItem *cancelButton;
@property (nonatomic, strong) DBManager* dbManager;
@end
@implementation EvaluationViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.dbManager = [[DBManager alloc] initWithDatabaseFilename:@"emokitDb.sqlite"];
NSLog(@"self.dbManager %@",self.dbManager.documentsDirectory);
[self loadProjectWithId:1];
}
- (IBAction)cancelButton:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 4;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
Screen * screen = [self.project.screens objectAtIndex:indexPath.row];
static NSString * cellIdentifier = @"EvaluationCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
return cell;
}
-(void)loadProjectWithId:(int)projectId {
}
@end
很简单:
您可以这样添加按钮:
而且你必须给按钮一个目标来移动 collectionView contentOffset...
- (void)viewDidLoad
{
[super viewDidLoad];
self.dbManager = [[DBManager alloc] initWithDatabaseFilename:@"emokitDb.sqlite"];
NSLog(@"self.dbManager %@",self.dbManager.documentsDirectory);
[self loadProjectWithId:1];
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(910, 400 , 100, 100);
button.backgroundColor =[UIColor blackColor];
[self.collectionView.superview addSubview:button];
[button addTarget:self action:@selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];
}
然后在选择器中你必须实现一个方法来改变 contentOffset
- (IBAction)changeContentOffset:(id)sender {
[self.collectionView setContentOffset:CGPointMake(nextCellXValue, 0) animated:YES]
}
我有一个 UICollectionView
,它有一个 UICollectionViewCell
的数组,每个单元格占据视图的 80%。
我想在每次按下时滚动到下一个单元格的屏幕上添加一个静态 UIButton
,我必须将按钮 subview
添加到 Parent 视图而不是 UICollectionView
因为它是静态的。
我的问题是:如何将叠加层添加到主视图上以及如何通过按下按钮以编程方式滚动视图?
我在哪里实施 collection 视图
@interface EvaluationViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UIBarButtonItem *cancelButton;
@property (nonatomic, strong) DBManager* dbManager;
@end
@implementation EvaluationViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.dbManager = [[DBManager alloc] initWithDatabaseFilename:@"emokitDb.sqlite"];
NSLog(@"self.dbManager %@",self.dbManager.documentsDirectory);
[self loadProjectWithId:1];
}
- (IBAction)cancelButton:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 4;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
Screen * screen = [self.project.screens objectAtIndex:indexPath.row];
static NSString * cellIdentifier = @"EvaluationCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
return cell;
}
-(void)loadProjectWithId:(int)projectId {
}
@end
很简单:
您可以这样添加按钮: 而且你必须给按钮一个目标来移动 collectionView contentOffset...
- (void)viewDidLoad
{
[super viewDidLoad];
self.dbManager = [[DBManager alloc] initWithDatabaseFilename:@"emokitDb.sqlite"];
NSLog(@"self.dbManager %@",self.dbManager.documentsDirectory);
[self loadProjectWithId:1];
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(910, 400 , 100, 100);
button.backgroundColor =[UIColor blackColor];
[self.collectionView.superview addSubview:button];
[button addTarget:self action:@selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];
}
然后在选择器中你必须实现一个方法来改变 contentOffset
- (IBAction)changeContentOffset:(id)sender {
[self.collectionView setContentOffset:CGPointMake(nextCellXValue, 0) animated:YES]
}