-
Notifications
You must be signed in to change notification settings - Fork 3
/
ERScrollSwitch.m
57 lines (46 loc) · 1.67 KB
/
ERScrollSwitch.m
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
//
// ERScrollSwitch.m
// PurePractice
//
// Created by Alex on 3/20/13.
// Copyright (c) 2013 Electronic Remedy, Inc. All rights reserved.
//
#import "ERScrollSwitch.h"
#import <QuartzCore/QuartzCore.h>
@implementation ERScrollSwitch
- (id)initWithFrame:(CGRect)frame image:(UIImage*)image thumbOffset:(NSInteger)thumbOffset delegate:(id<ERScrollSwitchDelegate>)delegate {
self = [super initWithFrame:frame];
self.switchDelegate = delegate;
[self setBackgroundColor:[UIColor whiteColor]];
self.thumbOffset = thumbOffset;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
[button setBackgroundColor:[UIColor whiteColor]];
[self addSubview:button];
[button setImage:image forState:UIControlStateNormal];
[button addTarget:self
action:@selector(toggle:)
forControlEvents:UIControlEventTouchUpInside];
self.contentSize = image.size;
self.layer.cornerRadius = image.size.height / 2.0; // completely rounded corners
self.scrollEnabled = NO;
return self;
}
- (void)setOn:(BOOL)on animated:(BOOL)animated {
[self setOn:on animated:animated toggle:YES];
}
- (void)setOn:(BOOL)on animated:(BOOL)animated toggle:(BOOL)toggle {
BOOL previousState = _on;
_on = on;
CGPoint scrollPoint = CGPointMake((self.on) ? 0 : self.thumbOffset, 0.0);
[self setContentOffset:scrollPoint animated:animated];
if (toggle && _on != previousState && self.switchDelegate) {
[self.switchDelegate didToggle:self];
}
}
- (void) setOn:(BOOL)on {
[self setOn:on animated:NO];
}
- (void)toggle:(id)sender {
[self setOn:!self.on animated:YES];
}
@end