码迷,mamicode.com
首页 > 其他好文 > 详细

initWithFrame和initWithCoder使用技巧,以及他们什么时候被调用,自定义控件必须具备三个方法initWithFrame和initWithCoder以及drawRect

时间:2014-09-11 22:19:52      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:color   io   os   使用   ar   for   2014   sp   cti   

//
//  MJLockView.m
//  03-手势解锁
//
//  Created by apple on 14-4-20.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "MJLockView.h"

@implementation MJLockView
//当你通过代码创建控件就会调用这个方法
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}
//当你通过storyboared或者xib中创建控件就会调用这个方法
- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self setup];
    }
    return self;
}

/**
 初始化
 */
- (void)setup
{
    for (int index = 0; index<9; index++) {
        // 创建按钮
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        btn.userInteractionEnabled = NO;
        
        btn.backgroundColor = [UIColor blueColor];
        
        // 设置默认的背景图片
        [btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
        
        // 设置选中时的背景图片(selected)
        [btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];
        
        // 添加按钮
        [self addSubview:btn];
    }
}

// 调整按钮的frame最好在这个方法里面
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    for (int index = 0; index<self.subviews.count; index++) {
        // 取出按钮
        UIButton *btn = self.subviews[index];
        
        // 设置frame
        CGFloat btnW = 74;
        CGFloat btnH = 74;
        
        int totalColumns = 3;
        int col = index % totalColumns;
        int row = index / totalColumns;
        CGFloat marginX = (self.frame.size.width - totalColumns * btnW) / (totalColumns + 1);
        CGFloat marginY = marginX;
        
        CGFloat btnX = marginX + col * (btnW + marginX);
        CGFloat btnY = row * (btnH + marginY);
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint pos = [touch locationInView:touch.view];
    
    for (UIButton *btn in self.subviews) {
        if (CGRectContainsPoint(btn.frame, pos)) {
            btn.selected = YES;
        }
    }
    
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint pos = [touch locationInView:touch.view];
    
    for (UIButton *btn in self.subviews) {
        if (CGRectContainsPoint(btn.frame, pos)) {
            btn.selected = YES;
        }
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesEnded:touches withEvent:event];
}

- (void)drawRect:(CGRect)rect
{
    
}

@end

initWithFrame和initWithCoder使用技巧,以及他们什么时候被调用,自定义控件必须具备三个方法initWithFrame和initWithCoder以及drawRect

标签:color   io   os   使用   ar   for   2014   sp   cti   

原文地址:http://www.cnblogs.com/xiaokanfengyu/p/3967336.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!