创建UIButton
// Create a button sized to our art UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0.0f, 0.0f, 300.0f, 233.0f); button.center = CGPointMake(160.0f, 140.0f); // Set up the button aligment properties button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; // Set the font and color [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted]; // Hard code carriage returns // button.titleLabel.font = [UIFont boldSystemFontOfSize:36.0f]; // [button setTitle:@"Word1\nWord2\nWord3" forState: UIControlStateNormal]; // Let label handle carriage returns button.titleLabel.font = [UIFont boldSystemFontOfSize:36.0f]; [button setTitle:@"Lorem Ipsum Dolor Sit" forState: UIControlStateNormal]; button.titleLabel.textAlignment = UITextAlignmentCenter; button.titleLabel.lineBreakMode = UILineBreakModeWordWrap; // Add action [button addTarget:self action:@selector(toggleButton:) forControlEvents: UIControlEventTouchUpInside];
创建一个动画
// Load Butterflies NSMutableArray *bflies = [NSMutableArray array]; UIImage *img; for (int i = 1; i <= 17; i++) { NSString *bfname = [NSString stringWithFormat:@"bf_%d.png", i]; if (img = [UIImage imageNamed:bfname]) [bflies addObject:img]; } UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 80.0f, 80.0f)]; [imageView setAnimationImages:bflies]; [imageView setAnimationDuration:1.2f]; [imageView startAnimating]; imageView.center = button.center;
一个UIButton翻转的例子
@implementation TestBedViewController - (IBAction) flip: (UIButton *) button { // Hide the view that's going away [self.view viewWithTag:BUTTON1].alpha = 1.0f; [self.view viewWithTag:BUTTON2].alpha = 1.0f; [button setAlpha:0.0f]; // Decide which animation to use UIViewAnimationTransition trans; trans = (button.tag == BUTTON1) ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight; // Animate the flip [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:1.0f]; [UIView setAnimationTransition:trans forView:[self.view viewWithTag:CLEARVIEW] cache:YES]; [[self.view viewWithTag:CLEARVIEW] exchangeSubviewAtIndex:0 withSubviewAtIndex:1]; [UIView commitAnimations]; }
UISilder的使用
// Create slider UISlider *slider = [[UISlider alloc] initWithFrame:baseFrame]; slider.center = CGPointMake(160.0f, 140.0f); slider.value = 0.0f; // Create the callbacks for touch, move, and release [slider addTarget:self action:@selector(startDrag:) forControlEvents:UIControlEventTouchDown]; [slider addTarget:self action:@selector(updateThumb:) forControlEvents:UIControlEventValueChanged]; [slider addTarget:self action:@selector(endDrag:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside]; // Present the slider [self.view addSubview:slider]; [self performSelector:@selector(updateThumb:) withObject:slider afterDelay:0.1f]; //创建图片的例子 - (UIImage *) createImageWithLevel: (float) aLevel { UIGraphicsBeginImageContext(CGSizeMake(40.0f, 100.0f)); CGContextRef context = UIGraphicsGetCurrentContext(); float INSET_AMT = 1.5f; // Create a filled rect for the thumb [[UIColor darkGrayColor] setFill]; CGContextAddRect(context, CGRectMake(INSET_AMT, 40.0f + INSET_AMT, 40.0f - 2.0f * INSET_AMT, 20.0f - 2.0f * INSET_AMT)); CGContextFillPath(context); // Outline the thumb [[UIColor whiteColor] setStroke]; CGContextSetLineWidth(context, 2.0f); CGContextAddRect(context, CGRectMake(2.0f * INSET_AMT, 40.0f + 2.0f * INSET_AMT, 40.0f - 4.0f * INSET_AMT, 20.0f - 4.0f * INSET_AMT)); CGContextStrokePath(context); // Create a filled ellipse for the indicator [[UIColor colorWithWhite:aLevel alpha:1.0f] setFill]; CGContextAddEllipseInRect(context, CGRectMake(0.0f, 0.0f, 40.0f, 40.0f)); CGContextFillPath(context); // Label with a number NSString *numstring = [NSString stringWithFormat:@"%0.1f", aLevel]; UIColor *textColor = (aLevel > 0.5f) ? [UIColor blackColor] : [UIColor whiteColor]; centerText(context, @"Georgia", 20.0f, numstring, CGPointMake(20.0f, 20.0f), textColor); // Outline the indicator circle [[UIColor grayColor] setStroke]; CGContextSetLineWidth(context, 3.0f); CGContextAddEllipseInRect(context, CGRectMake(INSET_AMT, INSET_AMT, 40.0f - 2.0f * INSET_AMT, 40.0f - 2.0f * INSET_AMT)); CGContextStrokePath(context); // Build and return the image UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return theImage; } //设置UISlider点击和正常两种状态 // create a new custom thumb image and use it for the highlighted state UIImage *customimg = [self createImageWithLevel:aSlider.value]; [aSlider setThumbImage: simpleThumbImage forState: UIControlStateNormal]; [aSlider setThumbImage: customimg forState: UIControlStateHighlighted]; previousValue = aSlider.value;
预留
预留
预留
预留
预留
预留