IOS 사용자 정의 UIButton 9 궁 격 효과
실제 열거 가 복잡 하지 않 으 면 두 세 단계 로 완성 한다.
1.너비 와 높이 정의(self.view)
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define JHRGB(r,g,b) [UIColor colorWithRed:(r/255.0) green:(g/255.0) blue:(b/255.0) alpha:1.0]
#define JHRandomColor JHRGB(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255))
2.구 궁 격 을 정의 하 는 문자 와 그림
@property (nonatomic, strong) NSArray * titlesArr;
@property (nonatomic, strong) UILabel * numberLab;
@property (nonatomic, strong) NSArray * titleimg;
-(NSArray *)titlesArr{
if (!_titlesArr) {
_titlesArr = @[@" ",@" ",@" ",@" ",@" ",@" ",@" ",@" ",@" "];
}
return _titlesArr;
}
-(NSArray *)titleimg{
if (!_titleimg) {
_titleimg = @[@"me",@"msg",@"meg",@"1",@"2",@"3",@"me",@"2",@"3"];
}
return _titleimg;
}
3.9 개의 UIBtton 데 이 터 를 순환 하고 관련 스타일 의 동적 조정
-(void)setButton{
NSInteger totalLoc = 3;//
CGFloat W = 50;//
CGFloat H = W;//
CGFloat margin=(self.view.frame.size.width-totalLoc * W)/(totalLoc+1);
for (NSInteger i = 0; i < self.titlesArr.count; i++) {//
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];//button
btn.frame = CGRectMake(100, 100, 80, 80);//button
[btn setTitle:self.titlesArr[i] forState:UIControlStateNormal];// button
[btn setBackgroundImage:[UIImage imageNamed:self.titleimg[i]] forState:UIControlStateNormal];//
[btn setTitleColor:[UIColor darkGrayColor] forState:0];//
[btn setImageEdgeInsets:UIEdgeInsetsMake(5, 25, 45, 25)];//
[btn setTitleEdgeInsets:UIEdgeInsetsMake(80, 0, 5, 0)];//
//btn.backgroundColor = [UIColor blueColor];
/* frame*/
NSInteger row = i / totalLoc;//
NSInteger loc = i % totalLoc;//
//0/3=0,1/3=0,2/3=0,3/3=1;
//0%3=0,1%3=1,2%3=2,3%3=0;
CGFloat X= margin + (margin + W) * loc;
CGFloat Y= margin + (margin + H) * row;
btn.frame = CGRectMake(X, Y, W, H);
// tag ( tag, button )
btn.tag = i;
[btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.lgView.imgview addSubview:btn];
}
}
4.버튼 을 클릭 한 이벤트 감청
-(void)clickBtn:(UIButton *)btn{
NSString *stringInt = [NSString stringWithFormat:@"%ld",(long)btn.tag];
btn.layer.transform = CATransform3DMakeScale(0.5*arc4random_uniform([stringInt floatValue]), 0.5*arc4random_uniform([stringInt floatValue]), 1);
self.numberLab.text = btn.titleLabel.text;
NSLog(@"%@wo dian ji l:",stringInt);
[UIView animateWithDuration:0.5 animations:^{
btn.layer.transform = CATransform3DMakeScale(1, 1, 1);
}];
}
요약:다음 논리 분석:
1.위 에 masonry 레이아웃 을 사 용 했 기 때문에 제 view 용 기 는 너비,높이 를 사용 하지 않 았 습 니 다.
2.먼저 View 용 기 를 정의 합 니 다.
3.용기 에 서 는 순환 체 에서 button 을 정의 하고 button 의 속성 을 설정 합 니 다.
4.관련 배열 을 정의 합 니 다.예 를 들 어:(텍스트,그림)
5.버튼 을 누 르 면 이벤트 트리거 함수;
상기 정보 가 이해 해 야 할 논리 에 대해 위의 복사 와 붙 여 넣 으 면 됩 니 다.프로젝트 는 직접 측정 할 수 있 습 니 다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
IOS에서 ReplayKit 및 RTC 사용 방법응용된 소리와 아나운서의 소리를 포함한다.이 두 가지 수요를 감안하여 우리는 스크린 공유를 하는 생방송에 필요한 미디어 흐름을 간단하게 분석할 수 있다. 만약 우리가 Audio App과 Audio Mic를 두 개의 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.