iOS开发中UISwitch按钮的使用方法简介

2020-01-14 17:11:51王冬梅

- (UIImage *)image:(UIImage*)image tintedWithColor:(UIColor *)tint 

 
    if (tint != nil) 
 {
  UIGraphicsBeginImageContext(image.size);
  
  //draw mask so the alpha is respected
  CGContextRef currentContext = UIGraphicsGetCurrentContext();
  CGImageRef maskImage = [image CGImage];
  CGContextClipToMask(currentContext, CGRectMake(0, 0, image.size.width, image.size.height), maskImage);
  CGContextDrawImage(currentContext, CGRectMake(0,0, image.size.width, image.size.height), image.CGImage);
  
  [image drawAtPoint:CGPointMake(0,0)];
  [tint setFill];
  UIRectFillUsingBlendMode(CGRectMake(0,0,image.size.width,image.size.height),kCGBlendModeColor);
  UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  
        return newImage;
    }
    else 
 {
        return image;
    }
}

 

-(void)setTintColor:(UIColor*)color
{
 if (color != tintColor)
 {
  [tintColor release];
  tintColor = [color retain];
  
  [self setMinimumTrackImage:[self image:[UIImage imageNamed:@"switchBlueBg.png"] tintedWithColor:tintColor] forState:UIControlStateNormal];
 }
 
}

- (void)setOn:(BOOL)turnOn animated:(BOOL)animated;
{
 on = turnOn;
 
 if (animated)
 {
  [UIView  beginAnimations:nil context:nil];
  [UIView setAnimationDuration:0.2];
 }
 
 if (on)
 {
  self.value = 1.0;
 }
 else 
 {
  self.value = 0.0;
 }
 
 if (animated)
 {
  [UIView commitAnimations]; 
 }
}

- (void)setOn:(BOOL)turnOn
{
 [self setOn:turnOn animated:NO];
}


- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
 NSLog(@"preendTrackingWithtouch");
 [super endTrackingWithTouch:touch withEvent:event];