南昌IOS培训
达内南昌中心

0571-56020830

热门课程

iOS多边形按键的创建方法

  • 时间:2016-02-23
  • 发布:南昌ios培训
  • 来源:慕课网


    南昌达内IOS培训专家认为想要创建一堆 多边形按钮,就需要一个抽象的类,可以由贝赛尔曲线创建按键,是UIButton的子类。


按键抽象类头文件


//
//  RLCShapeButton.h
//  duobianxing
//
//  Created by Realank on 16/2/16.
//  Copyright © 2016年 iMooc. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface RLCShapeButton : UIButton

- (instancetype)initWithPath:(UIBezierPath *)path andOrigin:(CGPoint)point;

@end


按键抽象类实现文件:


//
//  RLCShapeButton.m
//  duobianxing
//
//  Created by Realank on 16/2/16.
//  Copyright © 2016年 iMooc. All rights reserved.
//

#import "RLCShapeButton.h"

@interface RLCShapeButton ()

@property(nonatomic, strong) UIBezierPath *path;

@end
@implementation RLCShapeButton

- (instancetype)initWithPath:(UIBezierPath *)path andOrigin:(CGPoint)point{
    if (self = [super initWithFrame:CGRectMake(point.x, point.y, path.bounds.size.width, path.bounds.size.height)]) {
        _path = path;
        CAShapeLayer *shapLayer = [CAShapeLayer layer];
        shapLayer.path = self.path.CGPath;
        shapLayer.strokeColor = [UIColor redColor].CGColor;
        shapLayer.lineWidth = 5;
        shapLayer.fillColor = [UIColor clearColor].CGColor;
//        self.layer.mask = shapLayer;
        [self.layer addSublayer:shapLayer];
    }
    return self;
}

//覆盖方法,点击时判断点是否在path内,YES则响应,NO则不响应
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    BOOL res = [super pointInside:point withEvent:event];
    if (res)
    {
        if ([self.path containsPoint:point])
        {
            return YES;
        }
        return NO;
    }
    return NO;
}

@end


在ViewController中创建并使用这个按键类:


- (void)viewDidLoad {
    [super viewDidLoad];

    UIBezierPath* path = [[UIBezierPath alloc]init];
    [path moveToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(50, 50)];
    [path addLineToPoint:CGPointMake(100, 50)];
    [path addLineToPoint:CGPointMake(100,100)];
    [path addLineToPoint:CGPointMake(0, 100)];
    [path closePath];

    RLCShapeButton *button = [[RLCShapeButton alloc]initWithPath:path andOrigin:CGPointMake(100, 100)];
    [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

}

- (void)click {
    NSLog(@"click");
}


    按键的大小,根据创建时提供的贝赛尔曲线大小,自动设置 。




原文链接:http://www.imooc.com/article/4941
上一篇:达内:入门ios语言要懂得哪些知识?
下一篇:苹果发布iOS 9.3最新版本,再次调整

达内iOS班学员整班高薪就业,专科生最高薪资15000

达内iOS班学员就业喜人,最高薪资12000

795万高校毕业生创历史新高,2017届毕业生就业近况几何?

达内Linux学员毕业2周就业率96%,最高薪资10000元

选择城市和中心
贵州省

广西省

海南省