그런 식으로 텍스트의 크기를 움직일 수는 없다고 생각합니다. 이 작업을 수행하는 유일한 방법은 레이블의 스냅 샷보기를 작성하고 레이블에 해당보기를 추가 한 다음 애니메이션을 수행 한 다음 스냅 샷보기를 제거하는 것입니다. 이 코드는 작은 텍스트를 약간 아래로 이동하지만 라벨을 숨기고 이미지보기를 제거 할 때 아주 작은 움직임만으로도보기에는 아주 좋습니다. 레이블이 포함 된 작은보기의 크기는 185x36이고 레이블의 크기는 smallView의 각면에 대해 20이고 상단에는 8, 하단에는 7입니다. 코드에서 이미지 뷰에 동일한 제약 조건을 추가합니다.
@interface ViewController()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *widthCon; // width constraint on smallView
@property (weak,nonatomic) IBOutlet UIView *smallView; // view that the label is embedded in
@property (weak,nonatomic) IBOutlet UILabel *label;
@end
@implementation ViewController
- (IBAction)shrinkView:(id)sender {
UIView *snapshot = [self.label snapshotViewAfterScreenUpdates:YES];
[snapshot setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.smallView addSubview:snapshot];
[self.smallView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-20-[snapshot]-20-|" options:0 metrics:nil views:@{@"snapshot":snapshot}]];
NSLayoutConstraint *topCon = [NSLayoutConstraint constraintWithItem:snapshot attribute:NSLayoutAttributeTop relatedBy:0 toItem:self.smallView attribute:NSLayoutAttributeTop multiplier:1 constant:8];
NSLayoutConstraint *bottomCon = [NSLayoutConstraint constraintWithItem:self.smallView attribute:NSLayoutAttributeBottom relatedBy:0 toItem:snapshot attribute:NSLayoutAttributeBottom multiplier:1 constant:7];
[self.smallView addConstraints:@[topCon,bottomCon]];
[self.smallView layoutSubviews];
self.label.alpha = 0;
self.widthCon.constant = 100;
topCon.constant = 18;
bottomCon.constant = 10;
[UIView animateWithDuration:.5 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
self.label.alpha = 1;
[snapshot removeFromSuperview];
}];
}
편집 후 :
레이블도 끝 크기로 바로가는 것보다 아래로 애니메이션되도록 뷰를 애니메이션 할 수있는 방법이있다. 타이머를 사용하여 제약 조건을 직접 애니메이션화해야합니다 (WWDC 2012 비디오 "마스터 레이아웃 자동 레이아웃 우수 사례"에 대한 설명은 약 31 분을 참조하십시오). 이 작업은 라벨의 크기를 움직이게하지만 글꼴 크기가 급격하게 바뀌고 그 모양이 좋지 않습니다. 당신이 레이블에 있음을 뷰에 폭 제약 조건이 (그리고 라벨이 양측에 제약이있다) 경우에, 당신은이 작업을 수행 할 수 있습니다 나는 그것의 width
을 변경하여 UILabel
크기 조정 애니메이션 고투
-(IBAction)animateSizeChange:(id)sender {
[NSTimer scheduledTimerWithTimeInterval:.001 target:self selector:@selector(doStuff:) userInfo:Nil repeats:YES];
}
-(void)doStuff:(NSTimer *) aTimer {
self.widthCon.constant -= .2;
if (self.widthCon.constant <90) [aTimer invalidate];
}
타이머를 사용한 편집에 관한 코드는 불행히도 매우 불안해 보입니다. :/ –
스냅 샷 아이디어는 매우 해키하며 이렇게 접근하면 안됩니다. 생각하지 않습니까? –
@ Christian'fuzi'Orgler, 아니, 나는 그것이 전혀 해킹되지 않는다고 생각하지 않는다. Apple은 자신의 애니메이션 중 일부에서이 "트릭"을 사용합니다. 게다가, 대체 뭐야? 귀하의 첫 번째 의견에 관해서는 예, 그것은 내가 말했던 것입니다 - 나는 텍스트의 크기를 줄이지 않으려 고하는 경우 때때로 잘 작동하는보기의 하위보기를 움직일 수있는 방법을 보여주기 위해 포함 시켰습니다. – rdelmar