2014-04-01 23:18:30 +02:00
|
|
|
//
|
|
|
|
// MainViewController.m
|
|
|
|
// DragGestureRecognizer
|
|
|
|
//
|
|
|
|
// Created by Wojciech Nagrodzki on 01/04/14.
|
|
|
|
// Copyright (c) 2014 Wojciech Nagrodzki. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "MainViewController.h"
|
2014-04-01 23:20:46 +02:00
|
|
|
#import "NGDragGestureRecognizer.h"
|
|
|
|
|
2014-04-01 23:18:30 +02:00
|
|
|
|
|
|
|
@interface MainViewController ()
|
|
|
|
|
2014-04-01 23:20:46 +02:00
|
|
|
@property (strong, nonatomic) UIView * containerView;
|
|
|
|
@property (strong, nonatomic) UIView * draggableView;
|
|
|
|
|
2014-04-01 23:18:30 +02:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
2014-04-01 23:20:46 +02:00
|
|
|
@implementation MainViewController
|
2014-04-01 23:18:30 +02:00
|
|
|
|
|
|
|
- (void)viewDidLoad
|
|
|
|
{
|
|
|
|
[super viewDidLoad];
|
2014-04-01 23:20:46 +02:00
|
|
|
|
|
|
|
CGFloat diameter = 44;
|
|
|
|
|
|
|
|
for (NSInteger row = 1; row < 5; row++) {
|
|
|
|
for (NSInteger column = 1; column < 4; column++) {
|
|
|
|
|
|
|
|
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, diameter, diameter)];
|
|
|
|
view.center = CGPointMake(column * 80, row * 96);
|
|
|
|
view.backgroundColor = [UIColor whiteColor];
|
|
|
|
view.layer.cornerRadius = diameter / 2;
|
|
|
|
view.layer.borderColor = [UIColor colorWithRed:0.0902 green:0.5765 blue:0.7725 alpha:1.0000].CGColor;
|
|
|
|
view.layer.borderWidth = 3;
|
|
|
|
|
|
|
|
NGDragGestureRecognizer * gestureRecognizer = [[NGDragGestureRecognizer alloc] initWithTarget:self action:@selector(handleDragGesture:)];
|
|
|
|
[view addGestureRecognizer:gestureRecognizer];
|
|
|
|
[self.view addSubview:view];
|
|
|
|
}
|
|
|
|
}
|
2014-04-01 23:18:30 +02:00
|
|
|
}
|
|
|
|
|
2014-04-01 23:20:46 +02:00
|
|
|
- (void)handleDragGesture:(NGDragGestureRecognizer *)gestureRecognizer
|
2014-04-01 23:18:30 +02:00
|
|
|
{
|
2014-04-01 23:20:46 +02:00
|
|
|
UIView * draggedView = gestureRecognizer.view;
|
|
|
|
UIView * superview = draggedView.superview;
|
|
|
|
|
|
|
|
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
|
|
|
|
[gestureRecognizer setTranslation:draggedView.center inView:superview];
|
|
|
|
[superview bringSubviewToFront:draggedView];
|
|
|
|
draggedView.layer.borderColor = [UIColor colorWithRed:0.2784 green:0.7412 blue:0.9412 alpha:1.0000].CGColor;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gestureRecognizer.state == UIGestureRecognizerStateChanged) {
|
|
|
|
draggedView.center = [gestureRecognizer translationInView:superview];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gestureRecognizer.state == UIGestureRecognizerStateEnded || gestureRecognizer.state == UIGestureRecognizerStateCancelled) {
|
|
|
|
draggedView.layer.borderColor = [UIColor colorWithRed:0.0902 green:0.5765 blue:0.7725 alpha:1.0000].CGColor;
|
|
|
|
return;
|
|
|
|
}
|
2014-04-01 23:18:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|