Skip to content

Instantly share code, notes, and snippets.

@hirokim
Created December 15, 2014 01:46
Show Gist options
  • Select an option

  • Save hirokim/f51daca483878b37d9cc to your computer and use it in GitHub Desktop.

Select an option

Save hirokim/f51daca483878b37d9cc to your computer and use it in GitHub Desktop.
切り取り範囲の矩形を選択するクラス
#import <UIKit/UIKit.h>
@interface TrimmingSquareView : UIView
@property (nonatomic, readonly) CGRect trimmngRect;
- (void)resetPosition;
@end
#import "TrimmingSquareView.h"
#define DEFAULT_EDGE_SIZE 50
#define MIM_EDGE_SIZE 20
@interface TrimmingSquareView()
{
CGRect trimRange;
CGPoint lastTouchPoint;;
CGSize cornerRectSize;
CGRect cornerRectTopLeft;
CGRect cornerRectTopRight;
CGRect cornerRectBottomLeft;
CGRect cornerRectBottomRight;
BOOL isRangeResizing;
BOOL isRangeMoving;
BOOL isTouchedLeftCorner;
}
@end
@implementation TrimmingSquareView
@synthesize trimmngRect = trimRange;
/**
* 初期化
*
*/
- (instancetype)init
{
self = [super init];
if (self) {
isRangeResizing = NO;
isRangeMoving = NO;
trimRange = CGRectZero;
lastTouchPoint = CGPointZero;
cornerRectSize = CGSizeMake(10, 10);
self.backgroundColor = [UIColor clearColor];
}
return self;
}
/**
* 切り取り矩形範囲のリセット
*
*/
- (void)resetPosition
{
trimRange = CGRectMake((self.frame.size.width * 0.5) - (DEFAULT_EDGE_SIZE * 0.5),
(self.frame.size.height * 0.5) - (DEFAULT_EDGE_SIZE * 0.5),
DEFAULT_EDGE_SIZE,
DEFAULT_EDGE_SIZE);
[self setNeedsDisplay];
}
/**
* 切り取り矩形範囲の描画
*
*/
- (void)drawRect:(CGRect)rect {
// 切り取り範囲の表示
UIBezierPath *rangeRectPath = [UIBezierPath bezierPathWithRect:trimRange];
[[UIColor redColor] setStroke];
rangeRectPath.lineWidth = 0.5;
CGFloat dashPattern[2] = {4.0f, 1.0f};
[rangeRectPath setLineDash:dashPattern count:2 phase:0];
[rangeRectPath stroke];
// 切り取り範囲の角の四角部分
UIBezierPath *cornerRectPath;
// 左上
cornerRectTopLeft.size = cornerRectSize;
cornerRectTopLeft.origin = CGPointMake(trimRange.origin.x - (cornerRectSize.width / 2),
trimRange.origin.y - (cornerRectSize.height / 2));
cornerRectPath = [UIBezierPath bezierPathWithRect:cornerRectTopLeft];
[[UIColor redColor] setFill];
[cornerRectPath fill];
// 右上
cornerRectTopRight.size = cornerRectSize;
cornerRectTopRight.origin = CGPointMake((trimRange.origin.x + trimRange.size.width) - (cornerRectSize.width / 2),
trimRange.origin.y - (cornerRectSize.height / 2));
cornerRectPath = [UIBezierPath bezierPathWithRect:cornerRectTopRight];
[[UIColor redColor] setFill];
[cornerRectPath fill];
// 左下
cornerRectBottomLeft.size = cornerRectSize;
cornerRectBottomLeft.origin = CGPointMake(trimRange.origin.x - (cornerRectSize.width / 2),
(trimRange.origin.y + trimRange.size.height) - (cornerRectSize.height / 2));
cornerRectPath = [UIBezierPath bezierPathWithRect:cornerRectBottomLeft];
[[UIColor redColor] setFill];
[cornerRectPath fill];
// 右下
cornerRectBottomRight.size = cornerRectSize;
cornerRectBottomRight.origin = CGPointMake((trimRange.origin.x + trimRange.size.width) - (cornerRectSize.width / 2),
(trimRange.origin.y + trimRange.size.height) - (cornerRectSize.height / 2));
cornerRectPath = [UIBezierPath bezierPathWithRect:cornerRectBottomRight];
[[UIColor redColor] setFill];
[cornerRectPath fill];
}
/**
* タッチ開始
*
*/
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
lastTouchPoint = [[touches anyObject] locationInView:self];
if (CGRectContainsPoint(cornerRectTopRight, lastTouchPoint)
|| CGRectContainsPoint(cornerRectBottomRight, lastTouchPoint)) {
isTouchedLeftCorner = NO;
isRangeResizing = YES;
} else if (CGRectContainsPoint(cornerRectTopLeft, lastTouchPoint)
|| CGRectContainsPoint(cornerRectBottomLeft, lastTouchPoint)) {
isTouchedLeftCorner = YES;
isRangeResizing = YES;
} else if (CGRectContainsPoint(trimRange, lastTouchPoint)) {
isRangeMoving = YES;
}
}
/**
* タッチ中
*
*/
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPos = [[touches anyObject] locationInView:self];
if (isRangeResizing) {
[self resizeTrimmingRectToPoint:touchPos];
} else if (isRangeMoving) {
[self moveTrimmingRectToPoint:touchPos];
}
lastTouchPoint = touchPos;
}
/**
* タッチ終了
*
*/
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPos = [[touches anyObject] locationInView:self];
if (isRangeResizing) {
[self resizeTrimmingRectToPoint:touchPos];
} else if (isRangeMoving) {
[self moveTrimmingRectToPoint:touchPos];
}
isRangeResizing = NO;
isRangeMoving = NO;
}
/**
* 切り取り矩形範囲のサイズ変更
*
*/
- (void)resizeTrimmingRectToPoint:(CGPoint)touchPos
{
if (![self checkRangeOnBaseView]) {
[self setNeedsDisplay];
return;
}
float offsetX = touchPos.x - lastTouchPoint.x;
if (isTouchedLeftCorner) {
offsetX *= -1;
}
trimRange.origin.x -= offsetX / 2;
trimRange.origin.y -= offsetX / 2;
trimRange.size.width += offsetX;
trimRange.size.height += offsetX;
[self setNeedsDisplay];
}
/**
* 切り取り矩形範囲の移動
*
*/
- (void)moveTrimmingRectToPoint:(CGPoint)touchPos
{
if (![self checkRangeOnBaseView]) {
[self setNeedsDisplay];
return;
}
float offsetX = touchPos.x - lastTouchPoint.x;
float offsetY = touchPos.y - lastTouchPoint.y;
trimRange.origin.x += offsetX;
trimRange.origin.y += offsetY;
[self setNeedsDisplay];
}
/**
* 切り取り矩形範囲がベースのベースのビューをはみ出ないように
*
*/
- (BOOL)checkRangeOnBaseView
{
BOOL reslt = YES;
// 最小の大きさ
if (trimRange.size.width < MIM_EDGE_SIZE) {
trimRange.size.width = MIM_EDGE_SIZE;
trimRange.size.height = MIM_EDGE_SIZE;
reslt = NO;
}
// 左にはみ出ないように
if (trimRange.origin.x < 0) {
trimRange.origin.x = 0;
reslt = NO;
}
// 上にはみ出ないように
if (trimRange.origin.y < 0) {
trimRange.origin.y = 0;
reslt = NO;
}
// 右にはみ出ないように
if ((trimRange.origin.x + trimRange.size.width) > self.frame.size.width) {
trimRange.origin.x = self.frame.size.width - trimRange.size.width;
reslt = NO;
}
// 下にはみ出ないように
if ((trimRange.origin.y + trimRange.size.height) > self.frame.size.height) {
trimRange.origin.y = self.frame.size.height - trimRange.size.height;
reslt = NO;
}
return reslt;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment