Ios 刷新iAction的操作
我有一个Ios 刷新iAction的操作,ios,objective-c,uibutton,uicollectionview,refresh,Ios,Objective C,Uibutton,Uicollectionview,Refresh,我有一个ui按钮,它将我的ui收藏视图向右滚动。该按钮被添加到myUICollectionView的superview中,因此它会浮在单元格顶部。 但是我只能让ui操作发生一次。如何复制操作或刷新按钮以便再次使用。 这是我的密码: - (void)viewDidLoad { [super viewDidLoad]; UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.f
ui按钮
,它将我的ui收藏视图
向右滚动。该按钮被添加到myUICollectionView
的superview中,因此它会浮在单元格顶部。
但是我只能让ui操作发生一次。如何复制操作或刷新按钮以便再次使用。
这是我的密码:
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(910, 345 , 100, 100);
button.backgroundColor = [UIColor purpleColor];
[self.collectionView.superview addSubview:button];
[button addTarget:self action:@selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];
}
- (IBAction)changeContentOffset:(id)sender {
[self.collectionView setContentOffset:CGPointMake(910, -65) animated:YES];
}
您可以将该按钮添加到self.view
或
在您的-(UICollectionViewCell*)集合视图中:(UICollectionView*)集合视图cellForItemAtIndexPath:(NSIndexPath*)indexPath
方法只需将按钮添加到单元格内容视图[cell.contentView addSubview:button]代码>如果在iAction内的行中设置断点,是否只停止一次?根据您的问题,您希望在每次单击该按钮时向右滚动,但您的方法没有这样做
该方法在您第一次点击按钮时设置UICollectionView
的contentOffset
,但在其他时间,由于UICollectionView
已移动,因此您看不到任何更改。尝试将您的方法替换为:
- (IBAction)changeContentOffset:(id)sender {
CGPoint offset = self.collectionView;
offset.x += 910;
offset.y -= 65;
[self.collectionView setContentOffset:offset animated:YES];
}
您必须更改此方法以取决于collectionView的实际位置
self.collectionView.paginateEnabled = YES;
- (IBAction)changeContentOffset:(id)sender {
float x = cellWidth + self.collectionView.contentOffset.x;
[self.collectionView setContentOffset:CGPointMake(910, x) animated:YES];
}
每次单击按钮操作都会调用它。无论如何,在将按钮作为子视图添加到collectionView之前添加按钮操作。从collectionView中删除按钮的任何位置?将所有按钮代码放在cellForItemAtIndexPath而不是ViewDidLoad,并将按钮添加到contentView而不是collectionView.superview。它仍然会只滚动一次。我的按钮在工作,但它只滚动一次