I wanted to create Custom UISwitch in my iOS game. Initially I just waned to change size, font and color and for that I used UICustomSwitch from Hardy Macia found at here. It's quite easy to use and customisable, but then I wanted to switch to use custom image instead of standard one. For that I need to modify code a bit, below is the code which use custom image to create UISwitch. BTW this is how it looks
Header is same as original implementation, but I removed unnecessary properties and added left and right images required to display on and off images for switch.
Now you should be able to see UISwitch with custom images, we now need to add code required to on and off switch from code and change image accordingly.
Now finally we also have to add code to enable interaction.
Header is same as original implementation, but I removed unnecessary properties and added left and right images required to display on and off images for switch.
@interface UICustomSwitch : UISlider { BOOL on; UIImageView* leftImage; UIImageView* rightImage; BOOL touchedSelf; } @property(nonatomic,getter=isOn) BOOL on; @property (nonatomic, retain) UIImageView* leftImage; @property (nonatomic, retain) UIImageView* rightImage; - (void)setOn:(BOOL)on animated:(BOOL)animated; @endNow lets see implementation. Below is code for initialising the UISwitch with custom rect. You can also see we are adding On and Off image as left and right subview to switch.
-(id)initWithFrame:(CGRect)rect { if ((self=[super initWithFrame:rect])) { [self awakeFromNib]; } return self; } -(void)awakeFromNib { [super awakeFromNib]; self.backgroundColor = [UIColor clearColor]; [self setThumbImage:[Util imageNamed:@"switch"] forState:UIControlStateNormal]; [self setMinimumTrackTintColor:[UIColor clearColor]]; [self setMaximumTrackTintColor:[UIColor clearColor]]; self.minimumValue = 0; self.maximumValue = 1; self.continuous = NO; self.on = NO; self.value = 0.0; self.leftImage = [[UIImageView alloc] initWithFrame:self.frame]; self.leftImage.image = [Util imageNamed:@"on"]; [self addSubview: self.leftImage]; [self.leftImage release]; self.rightImage = [[UIImageView alloc] initWithFrame:self.frame]; self.rightImage.image = [Util imageNamed:@"off"]; [self addSubview: self.rightImage]; [self.rightImage release]; }
Now you should be able to see UISwitch with custom images, we now need to add code required to on and off switch from code and change image accordingly.
- (void)setOn:(BOOL)turnOn animated:(BOOL)animated; { on = turnOn; if (animated) { [UIView beginAnimations:@"UICustomSwitch" context:nil]; [UIView setAnimationDuration:0.2]; } if (on) { self.value = 1.0; self.rightImage.hidden = YES; self.leftImage.hidden = NO; } else { self.value = 0.0; self.rightImage.hidden = NO; self.leftImage.hidden = YES; } if (animated) { [UIView commitAnimations]; } }
Now finally we also have to add code to enable interaction.
- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { [super endTrackingWithTouch:touch withEvent:event]; touchedSelf = YES; [self setOn:on animated:YES]; } - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { [super touchesBegan:touches withEvent:event]; touchedSelf = NO; on = !on; } - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { [super touchesEnded:touches withEvent:event]; if (!touchedSelf) { [self setOn:on animated:YES]; [self sendActionsForControlEvents:UIControlEventValueChanged]; } }That's it, I hope this will help.
This comment has been removed by the author.
ReplyDeleteWow! this is Amazing! Do you know your hidden name meaning ? Click here to find your hidden name meaning
ReplyDeletenice article Video Player
ReplyDeleteThis was a useful post and I think it's fairly easy to see in the other reviews, so this post is well written and useful. Keep up the good work.
ReplyDelete4K Ultra HD Enabled Video Player
Optimize Media App for Improved 4K Playback
Powerful 4K Ultra HD Video Player
#1 4K UHD/HDR Video Player for Android (Phone/Tablet)
#1 4K UHD/HDR Media Player for Windows 10 PC & Tablet
#1 4K UHD/HDR Movie Player for iPhone/iPad
Powerfull 4K HDR Enabled Video Player
Download 4K HDR Video Player for Android App
Download 4K HDR Media Player for Windows 10 App
Download 4K HDR Movie Player for iPhone/iPad App
This article is amazingly helpful and intersting, Thanks for sharing this wonderful informative article here. I appreciate your hard work.
ReplyDelete- Web Design, Web Development, Digital Marketing & App Development Company
- 24 HTML Editors and WYSIWYG Editors for Web Design & Development
- Top 19 Front-End Frameworks for Responsive Web Design
- Why is SEO Important for any Brand or Business?
- How to Make a Responsive Website from Scratch (HTML & CSS)?
- Top 10 Social Bookmarking Sites List
- Top 10 Best Web Hosting Companies in USA
- Advantages and Disadvantages of Front-End Frameworks
- Social Bookmarking Website List
- Directory Submission Website List
Download Video Player | Windows Media Player | Best Media Player
ReplyDeleteNice Blog ,your blog is sharing unique information....
ReplyDeleteWe provide Web Design, Web Development, Digital Marketing (SEO, SEM, SMO, SMM), and Mobile App Development services.
* Online HTML Editor | Wysiwyg HTML Editor
* Technical SEO Checklist | Website Audit Checklist
* Career opportunities | Online Jobs From Home
* Web Designing Course | Free Online Courses with Certificates
* Web Design Services | Web Design Solutions
* Web Design Packages | Web Development Packages
For more information visit our website at Digital Marketing Services | Online Marketing Services