如何解决如果使用cellForItemAtIndexPath,则UICollectionView中的UIButton动画不能用于首次初始化
我在一个主窗口内包含一个集合视图(self.myCollectionView
),其中包含另一个视图。我将收集单元放在触摸结束的路径上。对于这一部分,代码可以正常工作。但是,当我尝试为单元格cell.btn
中的按钮设置动画时,直到重新初始化视图后,该按钮才起作用。
//touch ended
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[touches allObjects] firstObject];
CGPoint p = [self.window convertPoint:[touch locationInView:touch.window] toView:self.myCollectionView];
NSIndexPath *idx = [self.myCollectionView indexPathForItemAtPoint:p];
if (idx){
UICollectionViewCell *cell = [self.myCollectionView cellForItemAtIndexPath:idx];
//Successfully getting the cell
[self animateButton:cell.btn];
//This doesn't work until the view is re-init/reload
}
}
//The animation method
-(void)animateButton:(UIButton *)sender{
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
[anim setDuration:0.05];
[anim setRepeatCount:5];
[anim setAutoreverses:YES];
[anim setFromValue:[NSValue valueWithCGPoint:
CGPointMake(sender.center.x - 5,sender.center.y)]];
[anim setToValue:[NSValue valueWithCGPoint:
CGPointMake(sender.center.x + 5,sender.center.y)]];
[anim.layer removeAllAnimations];
[anim.layer addAnimation:shake forKey:@"position"];
}
我已经使用[cell.btn addTarget:self action:@selector(animateButton:) forControlEvents:UIControlEventTouchUpInside]
方法进行了测试,并且将sender
传递给animateButton:
时,它每次都能工作。
知道为什么吗?预先感谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。