IOS 사용자 정의 UIButton 9 궁 격 효과

3730 단어 IOS구 궁 격
이 글 은 개발 과정 에서 도 이런 수요 가 많 기 때문에 사용자 정의 구 궁 격 을 어떻게 쓰 는 지 설명 한다.따라서 UIButton 을 이용 하여 논술 한 것 이다.
실제 열거 가 복잡 하지 않 으 면 두 세 단계 로 완성 한다.
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.버튼 을 누 르 면 이벤트 트리거 함수;
상기 정보 가 이해 해 야 할 논리 에 대해 위의 복사 와 붙 여 넣 으 면 됩 니 다.프로젝트 는 직접 측정 할 수 있 습 니 다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기