2022-12-06 19:41:36 +08:00

126 lines
3.5 KiB
Objective-C

//
// QRPhotoAlbumButton.m
// Unity-iPhone
//
// Created by zhl on 2022/12/6.
//
#import "QRPhotoAlbumButton.h"
@implementation QRPhotoAlbumButton
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
_edgeColor = [UIColor whiteColor];
_fillColor = [UIColor darkGrayColor];
_edgeHighlightedColor = [UIColor whiteColor];
_fillHighlightedColor = [UIColor blackColor];
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGFloat width = rect.size.width;
CGFloat height = rect.size.height;
CGFloat center = width / 2;
CGFloat middle = height / 2;
CGFloat strokeLineWidth = 2;
// Colors
UIColor *paintColor = (self.state != UIControlStateHighlighted) ? _fillColor : _fillHighlightedColor;
UIColor *strokeColor = (self.state != UIControlStateHighlighted) ? _edgeColor : _edgeHighlightedColor;
// box
CGFloat cameraWidth = width * 0.6;
CGFloat cameraHeight = cameraWidth * 0.618;
CGFloat cameraX = center - cameraWidth / 2;
CGFloat cameraY = middle - cameraHeight / 2;
CGFloat cameraRadius = cameraWidth / 80;
UIBezierPath *boxPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(cameraX, cameraY, cameraWidth, cameraHeight) cornerRadius:cameraRadius];
// Camera lens
CGFloat circleSize = cameraHeight * 0.2;
CGFloat cirlceX = cameraX + circleSize;
CGFloat circleY = cameraY + circleSize;
UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(cirlceX, circleY, circleSize, circleSize)];
// triangle
UIBezierPath *linePath = [UIBezierPath bezierPath];
[linePath moveToPoint:CGPointMake(cameraX, cameraY + cameraHeight * 3 /4)];
[linePath addLineToPoint:CGPointMake(cirlceX, cameraY + cameraHeight / 2)];
[linePath addLineToPoint:CGPointMake(cameraX + cameraWidth * 2 / 5 , cameraY + cameraHeight * 4 / 5)];
[linePath addLineToPoint:CGPointMake(cameraX + cameraWidth * 7 / 10, cameraY + cameraHeight* 2 / 5)];
[linePath addLineToPoint:CGPointMake(cameraX + cameraWidth, cameraY + cameraHeight * 4 / 5)];
[linePath addLineToPoint:CGPointMake(cameraX + cameraWidth, cameraY + cameraHeight)];
[linePath addLineToPoint:CGPointMake(cameraX, cameraY + cameraHeight)];
[linePath closePath];
linePath.lineCapStyle = kCGLineCapRound;
linePath.lineJoinStyle = kCGLineJoinRound;
// drawing
[paintColor setFill];
// [boxPath fill];
[strokeColor setStroke];
boxPath.lineWidth = strokeLineWidth;
[boxPath stroke];
[paintColor setFill];
[circlePath fill];
[strokeColor setStroke];
circlePath.lineWidth = strokeLineWidth;
[circlePath stroke];
[paintColor setFill];
[linePath fill];
[strokeColor setStroke];
linePath.lineWidth = strokeLineWidth;
[linePath stroke];
}
// MARK: - UIResponder Methods
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[self setNeedsDisplay];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
[self setNeedsDisplay];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
[self setNeedsDisplay];
}
@end