我正在尝试创建自己的UIImageView处理程序。我设法将必要的触摸事件子类化。我还没想出如何添加自己的属性。我想添加一个UIColor属性来设置我的处理程序的笔触颜色。这是.h文件。
//
// imageViewController.h
// slate
//
// Created by House of Pawn on 11/15/17.
// Copyright © 2017 ma. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface imageViewController : UIImageView
@end
这是.m文件
//
// imageViewController.m
// slate
//
// Created by House of Pawn on 11/15/17.
// Copyright © 2017 ma. All rights reserved.
//
#import "imageViewController.h"
@interface imageViewController ()
@end
@implementation imageViewController
CGPoint lastPoint;
CGFloat red;
CGFloat green;
NSString const *key = @"my.very.unique.key";
CGFloat blue;
CGFloat brush;
CGFloat opacity;
UIColor* strokeColor;
BOOL mouseSwiped;
- (void)viewDidLoad {
// [super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
// [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self];
NSLog(@"Touches Began");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];
UIGraphicsBeginImageContext(self.frame.size);
[self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 30.0 );
CGFloat* components = CGColorGetComponents(_strokeColor.CGColor);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), components[0], components[1], components[2], 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.image = UIGraphicsGetImageFromCurrentImageContext();
[self setAlpha:1.0];
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(!mouseSwiped) {
UIGraphicsBeginImageContext(self.frame.size);
[self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 14.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 1, 0, 1);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
@end
我需要能够像我上面所说的那样将我的自定义UIImageView控制器类传递给UIColor。我想使用的变量是笔触颜色。我看过有关使用类别的帖子,但对我来说没有任何意义。如果有人可以帮助我实现这一点,那就太好了。
答案 0 :(得分:0)
要自定义控件,您应该使用IBInspectable&amp; IBDesignable
对于目标C: 使用此链接:https://useyourloaf.com/blog/ib-designable-custom-views-in-interface-builder/
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface imageViewController : UIImageView
@property (nonatomic) IBInspectable UIColor *strokeColor;
@end
对于Swift: 使用此链接:http://nshipster.com/ibinspectable-ibdesignable/
@IBInspectable var strokeColor: UIColor? {
didSet {
layer.borderColor = borderColor?.CGColor
}
}
谢谢。