UIButton
진행률 표시 줄처럼 작동하는 이 로딩을 달성하고 싶었습니다 . 진행률이 100 %에 도달 button.enabled = true
하면 버튼 이 활성화됩니다 .. 이미지를 사용하여 두 개의 상태 버튼을 수행하는 방법과 머티리얼 디자인 UIButton
파급 효과 와 같은 버튼에 대한 전환 기반 애니메이션을 수행하는 방법을 배웠지 만이 를 달성하는 방법을 알 수 없습니다.
다음은 이러한 종류의 버튼 동작이있는 앱의 스크린 샷입니다.
여기에 내가 의미하는 바를 이해하지 못하는 경우를 대비하여 예제를 해킹했습니다.
#import <UIKit/UIKit.h>
@interface ProgressButton : UIView
@property (nonatomic, strong) UIView *fillBar;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) NSLayoutConstraint *fillWidthConstraint;
-(void)setFillPercentage:(CGFloat)decimalPercentage Animated:(BOOL)animated;
@end
#import "ProgressButton.h"
@implementation ProgressButton
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
[self initViews];
[self initConstraints];
}
return self;
}
-(void)initViews
{
self.layer.cornerRadius = 5.0;
self.clipsToBounds = YES;
self.backgroundColor = [UIColor lightGrayColor];
_fillBar = [[UIView alloc] init];
_fillBar.backgroundColor = [UIColor colorWithRed:47.0/255.0 green:204.0/255.0 blue:112.0/255.0 alpha:1.0];
_label = [[UILabel alloc] init];
_label.text = @"Resend Activation Code";
_label.textColor = [UIColor whiteColor];
_label.textAlignment = NSTextAlignmentCenter;
_button = [[UIButton alloc] init];
_button.userInteractionEnabled = NO;
[self addSubview:_fillBar];
[self addSubview:_label];
[self addSubview:_button];
}
-(void)initConstraints
{
_fillBar.translatesAutoresizingMaskIntoConstraints = NO;
_label.translatesAutoresizingMaskIntoConstraints = NO;
_button.translatesAutoresizingMaskIntoConstraints = NO;
id views = @{
@"fillBar": _fillBar,
@"label": _label,
@"button": _button
};
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[label]|" options:0 metrics:nil views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[label]|" options:0 metrics:nil views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[button]|" options:0 metrics:nil views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[button]|" options:0 metrics:nil views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[fillBar]" options:0 metrics:nil views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[fillBar]|" options:0 metrics:nil views:views]];
_fillWidthConstraint = [NSLayoutConstraint constraintWithItem:_fillBar attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:0.0];
[self addConstraint:_fillWidthConstraint];
}
-(void)setFillPercentage:(CGFloat)decimalPercentage Animated:(BOOL)animated
{
NSTimeInterval duration = 0.0;
if(animated)
{
duration = 0.5;
}
self.fillWidthConstraint.constant = self.bounds.size.width * decimalPercentage;
[UIView animateWithDuration:duration animations:^{
[self layoutIfNeeded];
} completion:^(BOOL finished) {
self.button.userInteractionEnabled = self.fillWidthConstraint.constant >= self.bounds.size.width ? YES : NO;
}];
}
@end
보기 컨트롤러에서이 progressButton에 대한 속성을 만듭니다.
#import <UIKit/UIKit.h>
#import "ProgressButton.h"
@interface ViewController : UIViewController
@property (nonatomic, strong) ProgressButton *progressButton;
@end
그런 다음 ViewController.m 파일 selector
에서 초기화 코드의 버튼 터치 업 이벤트에를 첨부 할 수 있습니다 .
self.progressButton = [[ProgressButton alloc] init];
[self.progressButton.button addTarget:self action:@selector(buttonTapped) forControlEvents:UIControlEventTouchUpInside];
버튼의 진행률을 설정할 준비가 되었으면 0.0에서 1.0 사이의 10 진수 백분율을 전달합니다. 여기서 1.0은 100 %를 나타냅니다.
[self.progressButton setFillPercentage:0.5 Animated:YES];
다음은 전체 ViewController.m 파일입니다.
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initViews];
[self initConstraints];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.progressButton setFillPercentage:0.5 Animated:YES];
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.progressButton setFillPercentage:1.0 Animated:YES];
});
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)initViews
{
self.progressButton = [[ProgressButton alloc] init];
[self.progressButton.button addTarget:self action:@selector(buttonTapped) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.progressButton];
}
-(void)initConstraints
{
self.progressButton.translatesAutoresizingMaskIntoConstraints = NO;
id views = @{
@"progressButton": self.progressButton
};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[progressButton]-20-|" options:0 metrics:nil views:views]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.progressButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
}
-(void)buttonTapped
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Resend Action Code?" message:@"Are you sure?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionYes = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"resend activation placeholder code");
}];
UIAlertAction *actionNo = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:actionYes];
[alertController addAction:actionNo];
[self presentViewController:alertController animated:YES completion:nil];
}
@end
내 ViewController 예제를 사용하면 다음과 같이 끝납니다.
진행률 버튼 진행률이 100 %가 아닌 경우 버튼을 클릭 할 수 없습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다