【问题标题】:adding properties to custom classes向自定义类添加属性
【发布时间】:2017-11-16 04:08:27
【问题描述】:

我正在尝试创建自己的 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。我想使用的变量是描边颜色。我看过有关使用类别的帖子,但它们对我来说毫无意义。如果有人可以帮助我实现它,那就太好了。

【问题讨论】:

  • 仅供参考 - .m 文件中的所有这些变量都是全局变量。这些是不是实例变量。

标签: ios objective-c


【解决方案1】:

要自定义控件,你应该使用 IBInspectable & 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

对于斯威夫特: 使用此链接:http://nshipster.com/ibinspectable-ibdesignable/

@IBInspectable var strokeColor: UIColor? {
   didSet {
       layer.borderColor = borderColor?.CGColor
   }
}

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-09
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    相关资源
    最近更新 更多