我周围有一个UIButton
和两个UILabels
。现在,当您点击按钮时,两个标签会激活并互换其位置。除了这种情况之外,我想要动画并旋转按钮180度,然后在标签改变位置时回到原始位置。我怎样才能做到这一点?
这里是按钮点击处理程序的代码 -
NSIndexPath *homeToWorkCellIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
UITableViewCell *homeToWorkCell = [self.tableView cellForRowAtIndexPath:homeToWorkCellIndexPath];
UILabel *homeLabel = (UILabel *)[homeToWorkCell viewWithTag:1];
UIButton *switchButton = (UIButton *)[homeToWorkCell viewWithTag:2];
UILabel *workLabel = (UILabel *)[homeToWorkCell viewWithTag:3];
CGRect homeLabelRectFrame = homeLabel.frame;
CGRect workLabelRectFrame = workLabel.frame;
[UIView animateWithDuration:0.3 animations:^{
homeLabel.frame = workLabelRectFrame;
workLabel.frame = homeLabelRectFrame;
} completion:^(BOOL finished) {
// will switch the pickup points here
NSString *currentFromPickupPointId = _fromPickupPointId;
NSString *currentToPickupPointId = _toPickupPointId;
_fromPickupPointId = currentToPickupPointId;
_toPickupPointId = currentFromPickupPointId;
}];
我想在这个方法中做这个动画,但我没有太多的动画经验,因为我是iOS开发新手。
编辑: 正如答案中提到的,我尝试将按钮点击处理程序更改为 -
NSIndexPath *homeToWorkCellIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
UITableViewCell *homeToWorkCell = [self.tableView cellForRowAtIndexPath:homeToWorkCellIndexPath];
UILabel *homeLabel = (UILabel *)[homeToWorkCell viewWithTag:1];
UIButton *switchButton = (UIButton *)[homeToWorkCell viewWithTag:2];
UILabel *workLabel = (UILabel *)[homeToWorkCell viewWithTag:3];
CGRect homeLabelRectFrame = homeLabel.frame;
CGRect workLabelRectFrame = workLabel.frame;
// animation for the labels
[UIView animateWithDuration:0.3 animations:^{
homeLabel.frame = workLabelRectFrame;
workLabel.frame = homeLabelRectFrame;
} completion:^(BOOL finished) {
// will switch the pickup points here
NSString *currentFromPickupPointId = _fromPickupPointId;
NSString *currentToPickupPointId = _toPickupPointId;
_fromPickupPointId = currentToPickupPointId;
_toPickupPointId = currentFromPickupPointId;
}];
// animation block for the button
[UIView animateWithDuration:0.3/2 delay:0 options:UIViewAnimationOptionAutoreverse animations:^{
CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI);
switchButton.transform = transform;
}completion:^(BOOL finished) {
CGAffineTransform transform = CGAffineTransformMakeRotation(0);
switchButton.transform = transform;
}];
这会为按钮旋转设置动画。但标签交换的动画不再发生。不知道这里发生了什么。
如果评论中有不明确的内容,请告诉我。
答案 0 :(得分:3)
使用此动画块作为按钮以及其余代码:
[UIView animateWithDuration:0.3/2 //half the label animation duration
delay:0
options:UIViewAnimationOptionAutoreverse
animations:^{
CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI);
switchButton.transform = transform;
}
completion:^(BOOL finished) {
CGAffineTransform transform = CGAffineTransformIdentity;
switchButton.transform = transform;
}];
如果您要逐帧交换标签,请在自定义AutoLayout
上删除UITableViewCell
(Xib>文件检查器>(取消选中)使用Autolayout)
如果想继续在AutoLayout
上使用UITableViewCell
,那么您需要更改标签限制而不是框架。
通过约束更改来实现:
view
s NSLayoutConstraint
个对象constant
参数-layoutIfNeeded
在自定义.h
课程的 UITableViewCell
中(我已将其命名为CustomCell
)
@interface CustomCell : UITableViewCell
@property (nonatomic, strong) IBOutlet UIButton *btnTest;
@property (nonatomic, strong) IBOutlet UITextField *txtF_1;
@property (nonatomic, strong) IBOutlet UITextField *txtF_2;
//----txtF_1 constraints
//Leading Constraint
@property (nonatomic, strong) IBOutlet NSLayoutConstraint *constraint_1_left;
//Trailing Constraint
@property (nonatomic, strong) IBOutlet NSLayoutConstraint *constraint_1_right;
//----txtF_2 constraints
//Leading Constraint
@property (nonatomic, strong) IBOutlet NSLayoutConstraint *constraint_2_left;
//Trailing Constraint
@property (nonatomic, strong) IBOutlet NSLayoutConstraint *constraint_2_right;
@end
示例XIB
如下所示:
关联NSLayoutConstraint
商店
txtF_1
限制条件概述:
txtF_2
限制条件概述:
您的tableViewController
方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomCell"
owner:nil
options:nil]
objectAtIndex:0];
}
[cell.txtF_1 setText:@"left"];
[cell.txtF_2 setText:@"right"];
[cell.btnTest addTarget:self
action:@selector(btnTestAct:event:)
forControlEvents:UIControlEventTouchUpInside];
return cell;
}
-(void)btnTestAct:(UIButton *)sender event:(UIEvent *)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
CustomCell *cell = (CustomCell *)[self.tableView cellForRowAtIndexPath:indexPath];
//backup constraints
NSInteger i_left = cell.constraint_1_left.constant;
NSInteger i_right = cell.constraint_1_right.constant;
//swap constraints
cell.constraint_1_left.constant = cell.constraint_2_left.constant;
cell.constraint_1_right.constant = cell.constraint_2_right.constant;
cell.constraint_2_left.constant = i_left;
cell.constraint_2_right.constant = i_right;
CGFloat f_animationTime = 0.3;
//textField animation
[UIView animateWithDuration:f_animationTime
animations:^{
//animate constraint changes
[self.view layoutIfNeeded];
}];
//button animation
[UIView animateWithDuration:f_animationTime/2
delay:0
options:UIViewAnimationOptionAutoreverse
animations:^{
CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI);
sender.transform = transform;
}
completion:^(BOOL finished) {
CGAffineTransform transform = CGAffineTransformIdentity;
sender.transform = transform;
}];
}
答案 1 :(得分:0)
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.35];
button.transform = CGAffineTransformMakeRotation(degreesToRadians(45));
[UIView commitAnimations];
不要忘记定义: #define degreesToRadians(degrees)(M_PI * degrees / 180.0)