Skip to content

Instantly share code, notes, and snippets.

@bigeyex
Created January 7, 2016 10:32
Show Gist options
  • Save bigeyex/a7e86ab88236b7b5a830 to your computer and use it in GitHub Desktop.
Save bigeyex/a7e86ab88236b7b5a830 to your computer and use it in GitHub Desktop.
"Extended UI Kit"
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface MBFlipImageView : UIImageView
@property (nonatomic) IBInspectable UIImage *onImage;
@property (nonatomic) IBInspectable UIImage *offImage;
- (void)flipOn;
- (void)flipOff;
- (void)setFlip:(bool)value;
@end
#import "MBFlipImageView.h"
@implementation MBFlipImageView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (void)flipOn{
self.image = self.onImage;
}
- (void)flipOff{
self.image = self.offImage;
}
- (void)setFlip:(bool)value{
if(value){
[self flipOn];
}
else{
[self flipOff];
}
}
@end
#import <UIKit/UIKit.h>
@interface MBRoundedShadowedView : UIView
@property IBInspectable CGFloat cornerRadius;
@property IBInspectable CGFloat shadowOpacity;
@property IBInspectable (nonatomic) UIColor *shadowColor;
@property IBInspectable CGFloat shadowRadius;
@property IBInspectable CGSize shadowOffset;
@end
#import "MBRoundedShadowedView.h"
@implementation MBRoundedShadowedView
- (void)layoutSubviews{
if(_cornerRadius){
self.layer.cornerRadius = self.cornerRadius;
self.clipsToBounds = NO;
}
if(_shadowOpacity){
self.layer.shadowColor = self.shadowColor.CGColor;
self.layer.shadowOffset = self.shadowOffset;
self.layer.shadowOpacity = self.shadowOpacity;
self.layer.shadowRadius = self.shadowRadius;
}
}
@end
#import <UIKit/UIKit.h>
#define MB_TELL_WEBVIEW(owner, ...) [owner tellWebview:@[__VA_ARGS__]]
#define MB_TELL_WEBVIEW_UNQUOTED(owner, ...) [owner tellWebviewUnquoted:@[__VA_ARGS__]]
#define MB_TELL_WEBVIEW_WITH_FUNCTION(owner, functionName, ...) [owner tellWebviewWithFunctionName:functionName argument:@[__VA_ARGS__]]
@interface MBSharedWebview : UIView
@property (nonatomic, weak) UIWebView *webview;
- (void)tellWebview:(NSArray<NSString*> *)arguments;
- (void)tellWebviewUnquoted:(NSArray<NSString*> *)arguments;
- (void)tellWebviewWithFunctionName:(NSString*)functionName argument:(NSArray<NSString*> *)arguments;
@end
#import "MBSharedWebview.h"
@implementation MBSharedWebview
- (instancetype)init{
self = [super init];
if(self){
}
return self;
}
- (void)awakeFromNib{
static UIWebView* sharedWebview = nil;
if(sharedWebview == nil){
sharedWebview = [[UIWebView alloc] init];
}
[self addSubview:sharedWebview];
self.webview = sharedWebview;
}
- (void)layoutSubviews{
self.webview.frame = self.frame;
}
- (void)tellWebview:(NSArray<NSString*> *)arguments{
NSMutableArray<NSString*> *quotedArguments = [NSMutableArray array];
for (NSString* argument in arguments) {
[quotedArguments addObject:[NSString stringWithFormat:@"\"%@\"", argument]];
}
[self tellWebviewUnquoted:quotedArguments];
}
- (void)tellWebviewUnquoted:(NSArray<NSString*> *)arguments{
[self tellWebviewWithFunctionName:@"receiveDeviceData" argument:arguments];
}
- (void)tellWebviewWithFunctionName:(NSString*)functionName argument:(NSArray<NSString*> *)arguments{
NSString *requestString = [NSString stringWithFormat:@"%@(%@)", functionName,[arguments componentsJoinedByString:@","]];
#ifdef ENABLE_BLOCKLY_LOG
DDLogInfo(@"tell blockly: %@", requestString);
#endif
[self.webview stringByEvaluatingJavaScriptFromString:requestString];
}
@end
#import <UIKit/UIKit.h>
@interface MBTypewriterLabel : UILabel
@property float typingInterval;
@end
#import "MBTypewriterLabel.h"
@implementation MBTypewriterLabel{
NSString *_targetText;
int _currentDisplayIndex;
NSTimer *_displayTimer;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if(self){
self.typingInterval = 0.05;
}
return self;
}
- (void)setText:(NSString *)text{
_targetText = text;
_currentDisplayIndex = 0;
[_displayTimer invalidate];
_displayTimer = [NSTimer scheduledTimerWithTimeInterval:self.typingInterval target:self selector:@selector(onDisplayTimer) userInfo:nil repeats:YES];
}
- (NSString *)text{
return _targetText;
}
- (void)onDisplayTimer{
_currentDisplayIndex++;
if(_currentDisplayIndex <= _targetText.length){
NSString *currentText = [_targetText substringToIndex:_currentDisplayIndex];
[super setText: currentText];
}
else{
[_displayTimer invalidate];
_displayTimer = nil;
}
}
@end
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface YUDashedRoundedBorderView : UIView
@property (nonatomic) IBInspectable CGFloat borderWidth;
@property (nonatomic) IBInspectable CGFloat cornerRadius;
@property (nonatomic) IBInspectable UIColor *borderColor;
@property (nonatomic) IBInspectable NSInteger dashPattern1;
@property (nonatomic) IBInspectable NSInteger dashPattern2;
@end
#import "YUDashedRoundedBorderView.h"
@implementation YUDashedRoundedBorderView
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if(self){
}
return self;
}
- (void)drawRect:(CGRect)rect{
[super drawRect:rect];
CGFloat cornerRadius = self.cornerRadius;
CGFloat borderWidth = self.borderWidth;
NSInteger dashPattern1 = self.dashPattern1;
NSInteger dashPattern2 = self.dashPattern2;
UIColor *lineColor = self.borderColor;
//drawing
CGRect frame = self.bounds;
CAShapeLayer *_shapeLayer = [CAShapeLayer layer];
//creating a path
CGMutablePathRef path = CGPathCreateMutable();
//drawing a border around a view
CGPathMoveToPoint(path, NULL, 0, frame.size.height - cornerRadius);
CGPathAddLineToPoint(path, NULL, 0, cornerRadius);
CGPathAddArc(path, NULL, cornerRadius, cornerRadius, cornerRadius, M_PI, -M_PI_2, NO);
CGPathAddLineToPoint(path, NULL, frame.size.width - cornerRadius, 0);
CGPathAddArc(path, NULL, frame.size.width - cornerRadius, cornerRadius, cornerRadius, -M_PI_2, 0, NO);
CGPathAddLineToPoint(path, NULL, frame.size.width, frame.size.height - cornerRadius);
CGPathAddArc(path, NULL, frame.size.width - cornerRadius, frame.size.height - cornerRadius, cornerRadius, 0, M_PI_2, NO);
CGPathAddLineToPoint(path, NULL, cornerRadius, frame.size.height);
CGPathAddArc(path, NULL, cornerRadius, frame.size.height - cornerRadius, cornerRadius, M_PI_2, M_PI, NO);
//path is set as the _shapeLayer object's path
_shapeLayer.path = path;
CGPathRelease(path);
_shapeLayer.backgroundColor = [[UIColor clearColor] CGColor];
_shapeLayer.frame = frame;
_shapeLayer.masksToBounds = NO;
[_shapeLayer setValue:[NSNumber numberWithBool:NO] forKey:@"isCircle"];
_shapeLayer.fillColor = [[UIColor clearColor] CGColor];
_shapeLayer.strokeColor = [lineColor CGColor];
_shapeLayer.lineWidth = borderWidth;
_shapeLayer.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInteger:dashPattern1], [NSNumber numberWithInteger:dashPattern2], nil];
_shapeLayer.lineCap = kCALineCapSquare;
//_shapeLayer is added as a sublayer of the view, the border is visible
[self.layer addSublayer:_shapeLayer];
self.layer.cornerRadius = cornerRadius;
}
@end
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface YURoundedButton : UIButton
@property IBInspectable CGFloat cornerRadius;
@end
#import "YURoundedButton.h"
#import <QuartzCore/QuartzCore.h>
@implementation YURoundedButton
- (void)layoutSubviews{
[super layoutSubviews];
self.layer.cornerRadius = self.cornerRadius;
self.clipsToBounds = YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment