zoukankan      html  css  js  c++  java
  • 自定义索引--秀清

    //
    //  ABELTableViewIndex.h
    //  ABELTableViewDemo
    //
    //  Created by abel on 14-4-28.
    //  Copyright (c) 2014年 abel. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @protocol BATableViewIndexDelegate;
    
    @interface BATableViewIndex : UIView
    @property (nonatomic, strong) NSArray *indexes;
    @property (nonatomic, weak) id <BATableViewIndexDelegate> tableViewIndexDelegate;
    @end
    
    @protocol BATableViewIndexDelegate <NSObject>
    
    /**
     *  触摸到索引时触发
     *
     *  @param tableViewIndex 触发didSelectSectionAtIndex对象
     *  @param index          索引下标
     *  @param title          索引文字
     */
    - (void)tableViewIndex:(BATableViewIndex *)tableViewIndex didSelectSectionAtIndex:(NSInteger)index withTitle:(NSString *)title;
    
    /**
     *  开始触摸索引
     *
     *  @param tableViewIndex 触发tableViewIndexTouchesBegan对象
     */
    - (void)tableViewIndexTouchesBegan:(BATableViewIndex *)tableViewIndex;
    /**
     *  触摸索引结束
     *
     *  @param tableViewIndex
     */
    - (void)tableViewIndexTouchesEnd:(BATableViewIndex *)tableViewIndex;
    
    /**
     *  TableView中右边右边索引title
     *
     *  @param tableViewIndex 触发tableViewIndexTitle对象
     *
     *  @return 索引title数组
     */
    - (NSArray *)tableViewIndexTitle:(BATableViewIndex *)tableViewIndex;
    
    @end
    
    
    
    //
    //  ABELTableViewIndex.m
    //  ABELTableViewDemo
    //
    //  Created by abel on 14-4-28.
    //  Copyright (c) 2014年 abel. All rights reserved.
    //
    
    #import "BATableViewIndex.h"
    
    
    
    #define RGB(r,g,b,a)  [UIColor colorWithRed:(double)r/255.0f green:(double)g/255.0f blue:(double)b/255.0f alpha:a]
    
    @interface BATableViewIndex (){
        BOOL isLayedOut;
        CAShapeLayer *shapeLayer;
        CGFloat letterHeight;
    }
    
    @property (nonatomic, strong) NSArray *letters;
    
    @end
    
    
    @implementation BATableViewIndex
    
    - (id)initWithFrame:(CGRect)frame{
        if (self = [super initWithFrame:frame]) {
            self.backgroundColor = [UIColor clearColor];
        }
        return self;
    }
    
    - (void)setup{
        shapeLayer = [CAShapeLayer layer];
        shapeLayer.lineWidth = 1.0f;
        shapeLayer.fillColor = [UIColor clearColor].CGColor;
        shapeLayer.lineJoin = kCALineCapSquare;
        shapeLayer.strokeColor = [[UIColor clearColor] CGColor];
        shapeLayer.strokeEnd = 1.0f;
        self.layer.masksToBounds = NO;
    }
    
    - (void)setTableViewIndexDelegate:(id<BATableViewIndexDelegate>)tableViewIndexDelegate
    {
        _tableViewIndexDelegate = tableViewIndexDelegate;
        self.letters = [self.tableViewIndexDelegate tableViewIndexTitle:self];
        isLayedOut = NO;
        [self layoutSubviews];
    }
    
    - (void)layoutSubviews{
        [super layoutSubviews];
        [self setup];
        
        if (!isLayedOut){
            
            [self.layer.sublayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)];
            
            shapeLayer.frame = (CGRect) {.origin = CGPointZero, .size = self.layer.frame.size};
            UIBezierPath *bezierPath = [UIBezierPath bezierPath];
            [bezierPath moveToPoint:CGPointZero];
            [bezierPath addLineToPoint:CGPointMake(0, self.frame.size.height)];
            letterHeight = 16; //self.frame.size.height / [letters count];
            CGFloat fontSize = 12;
            if (letterHeight < 14){
                fontSize = 11;
            }
            
            [self.letters enumerateObjectsUsingBlock:^(NSString *letter, NSUInteger idx, BOOL *stop) {
                CGFloat originY = idx * letterHeight;
                CATextLayer *ctl = [self textLayerWithSize:fontSize
                                                    string:letter
                                                  andFrame:CGRectMake(0, originY, self.frame.size.width, letterHeight)];
                [self.layer addSublayer:ctl];
                [bezierPath moveToPoint:CGPointMake(0, originY)];
                [bezierPath addLineToPoint:CGPointMake(ctl.frame.size.width, originY)];
            }];
            
            shapeLayer.path = bezierPath.CGPath;
            [self.layer addSublayer:shapeLayer];
            
            isLayedOut = YES;
        }
    }
    
    - (CATextLayer*)textLayerWithSize:(CGFloat)size string:(NSString*)string andFrame:(CGRect)frame{
        CATextLayer *tl = [CATextLayer layer];
        [tl setFont:@"ArialMT"];
        [tl setFontSize:size];
        [tl setFrame:frame];
        [tl setAlignmentMode:kCAAlignmentCenter];
        [tl setContentsScale:[[UIScreen mainScreen] scale]];
        [tl setForegroundColor:RGB(168, 168, 168, 1).CGColor];
        [tl setString:string];
        return tl;
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        [super touchesBegan:touches withEvent:event];
        [self sendEventToDelegate:event];
        [self.tableViewIndexDelegate tableViewIndexTouchesBegan:self];
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        [super touchesMoved:touches withEvent:event];
        [self sendEventToDelegate:event];
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self.tableViewIndexDelegate tableViewIndexTouchesEnd:self];
    }
    
    - (void)sendEventToDelegate:(UIEvent*)event{
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint point = [touch locationInView:self];
        
        NSInteger indx = ((NSInteger) floorf(point.y) / letterHeight);
        
        if (indx< 0 || indx > self.letters.count - 1) {
            return;
        }
        
        [self.tableViewIndexDelegate tableViewIndex:self didSelectSectionAtIndex:indx withTitle:self.letters[indx]];
    }
    
    @end
  • 相关阅读:
    define的用法
    MySQL索引使用方法和性能优化
    自己写的一个Js小插件
    .net处理JSON简明教程
    史上最全的ASP.NET MVC路由配置,以后RouteConfig再弄不懂神仙都难救你啦~
    Python面向对象之-反射
    Python内置函数之classmetho staticmethod
    Python内置函数之-property
    python面向对象三大特性-多态
    python面向对象三大特性之封装
  • 原文地址:https://www.cnblogs.com/sixindev/p/4730727.html
Copyright © 2011-2022 走看看