-
-
Save alexrepty/407457 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MiniToggle.h | |
// MiniToggle | |
// | |
// Created by jtrim on 2/12/10. | |
/* | |
MiniToggle.h & MiniToggle.m: | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR | |
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY | |
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
// | |
#import <UIKit/UIKit.h> | |
@class MiniToggle; | |
@protocol CSMiniToggleDelegate <NSObject> | |
- (void)miniToggle:(MiniToggle *)miniToggle didToggleToState:(BOOL)state; | |
@end | |
@interface MiniToggle : UIButton | |
{ | |
UIImage *bg; | |
UIImage *toggleImg; | |
UIImageView *toggleView; | |
BOOL isOn; | |
id<CSMiniToggleDelegate> delegate; | |
} | |
@property (nonatomic) BOOL isOn; | |
@property (nonatomic, retain) id<CSMiniToggleDelegate> delegate; | |
- (void)toggleOff:(BOOL)animated; | |
- (void)toggleOn:(BOOL)animated; | |
- (void)toggle; | |
- (void)touchToggle:(id)sender; | |
@end | |
// | |
// MiniToggle.m | |
// MiniToggle | |
// | |
// Created by jtrim on 2/12/10. | |
// | |
#import "MiniToggle.h" | |
#define OFF_X 36.5f | |
#define ON_X 8.5f | |
#define TOGGLE_Y 6.5f | |
@implementation MiniToggle | |
@synthesize isOn, delegate; | |
- (void)dealloc | |
{ | |
[toggleView release]; | |
[super dealloc]; | |
} | |
- (id)initWithFrame:(CGRect)frame | |
{ | |
bg = [UIImage imageNamed:@"mt_background.png"]; | |
toggleImg = [UIImage imageNamed:@"mt_toggle.png"]; | |
CGRect newFrame = CGRectMake(frame.origin.x, frame.origin.y, bg.size.width, bg.size.height); | |
if (self = [super initWithFrame:newFrame]) | |
{ | |
[self setOpaque:NO]; | |
[self setBackgroundColor:[UIColor clearColor]]; | |
isOn = NO; | |
[self addTarget:self action:@selector(touchToggle:) forControlEvents:UIControlEventTouchUpInside]; | |
} | |
return self; | |
} | |
-(void) drawRect:(CGRect)rect | |
{ | |
[bg drawInRect:CGRectMake(0, 0, bg.size.width, bg.size.height)]; | |
toggleView = [[UIImageView alloc] initWithImage:toggleImg]; | |
CGRect toggleFrame = CGRectMake(2, 1, toggleView.frame.size.width, toggleView.frame.size.height); | |
[toggleView setFrame:toggleFrame]; | |
// set up layers | |
CALayer *toggleLayer = [[CALayer alloc] init]; | |
[toggleLayer addSublayer:[toggleView layer]]; | |
[[toggleView layer]setPosition:CGPointMake(OFF_X, TOGGLE_Y)]; | |
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; | |
CGMutablePathRef path = CGPathCreateMutable(); | |
CGPathAddEllipseInRect(path, nil, CGRectMake(0, 0, 13, 13)); | |
CGPathAddEllipseInRect(path, nil, CGRectMake(30, 0, 13, 13)); | |
CGPathAddRect(path, nil, CGRectMake(6, 0, 30, 13)); | |
[maskLayer setPath:path]; | |
[maskLayer setFillColor:[[UIColor greenColor] CGColor]]; | |
[toggleLayer setMask:maskLayer]; | |
[maskLayer release]; | |
[toggleLayer setPosition:CGPointMake(2, 2)]; | |
[[self layer] addSublayer:toggleLayer]; | |
if (isOn) | |
[self toggleOn:NO]; | |
} | |
- (void)toggleOn:(BOOL)animated | |
{ | |
isOn = YES; | |
if (animated) | |
{ | |
[UIView beginAnimations:nil context:nil]; | |
[UIView setAnimationDuration:.168]; | |
} | |
CGPoint newPoint = CGPointMake(ON_X, TOGGLE_Y); | |
[[toggleView layer] setPosition:newPoint]; | |
if (animated) | |
[UIView commitAnimations]; | |
} | |
- (void)toggleOff:(BOOL)animated | |
{ | |
isOn = NO; | |
if (animated) | |
{ | |
[UIView beginAnimations:nil context:nil]; | |
[UIView setAnimationDuration:.173]; | |
} | |
CGPoint newPoint = CGPointMake(OFF_X, TOGGLE_Y); | |
[[toggleView layer] setPosition:newPoint]; | |
if (animated) | |
[UIView commitAnimations]; | |
} | |
- (void)toggle | |
{ | |
if (isOn) | |
[self toggleOff:YES]; | |
else | |
[self toggleOn:YES]; | |
if ([delegate respondsToSelector:@selector(miniToggle:didToggleToState:)]) | |
[delegate miniToggle:self didToggleToState:isOn]; | |
} | |
- (void)touchToggle:(id)sender | |
{ | |
[self toggle]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment