summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/SDLPinchGesture.m
blob: d1ea6e63cb4a0bbf19d32603e3c842dc59706fac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
//  SDLPinchGesture.m
//  SmartDeviceLink-iOS
//
//  Created by Muller, Alexander (A.) on 6/14/16.
//  Copyright © 2016 smartdevicelink. All rights reserved.
//

#import "SDLPinchGesture.h"

#import "CGPoint_Util.h"
#import "SDLTouch.h"

NS_ASSUME_NONNULL_BEGIN

@implementation SDLPinchGesture

@synthesize distance = _distance;
@synthesize center = _center;

- (instancetype)initWithFirstTouch:(SDLTouch *)firstTouch secondTouch:(SDLTouch *)secondTouch {
    self = [super init];
    if (!self) {
        return nil;
    }

    _firstTouch = firstTouch;
    _secondTouch = secondTouch;
    _distance = -1;
    _center = CGPointZero;

    return self;
}

#pragma mark - Setters
- (void)setFirstTouch:(SDLTouch *)firstTouch {
    if (firstTouch.identifier == SDLTouchIdentifierFirstFinger) {
        _firstTouch = firstTouch;
        [self sdl_invalidate];
    }
}

- (void)setSecondTouch:(SDLTouch *)secondTouch {
    if (secondTouch.identifier == SDLTouchIdentifierSecondFinger) {
        _secondTouch = secondTouch;
        [self sdl_invalidate];
    }
}

#pragma mark - Getters
- (CGFloat)distance {
    if (_distance == -1) {
        _distance = CGPointDistanceBetweenPoints(self.firstTouch.location,
                                                 self.secondTouch.location);
    }
    return _distance;
}

- (CGPoint)center {
    if (CGPointEqualToPoint(_center, CGPointZero)) {
        _center = CGPointCenterOfPoints(self.firstTouch.location,
                                        self.secondTouch.location);
    }
    return _center;
}

- (BOOL)isValid {
    return (self.firstTouch != nil) && (self.secondTouch != nil);
}

#pragma mark - Private
- (void)sdl_invalidate {
    _distance = -1;
    _center = CGPointZero;
}

@end

NS_ASSUME_NONNULL_END