2010年7月27日 星期二

iOS 開發教學 - 使用 UIToolbar + Image Button

之前學習時很偷懶,一直覺得有了 Navigation controller 和 Tabbar controller 後,總覺得畫面已經沒甚麼好擺的,所以遲遲沒有試試 UIToolbar ,這次就順手筆記一下囉。

參考資料:


程式碼:

@interface UIImage (Extras)

- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize;

@end

@implementation UIImage (Extras)

- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
{

   UIImage *sourceImage = self;
   UIImage *newImage = nil;      
   CGSize imageSize = sourceImage.size;
   CGFloat width = imageSize.width;
   CGFloat height = imageSize.height;
   CGFloat targetWidth = targetSize.width;
   CGFloat targetHeight = targetSize.height;
   CGFloat scaleFactor = 0.0;
   CGFloat scaledWidth = targetWidth;
   CGFloat scaledHeight = targetHeight;
   CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
 
   if (CGSizeEqualToSize(imageSize, targetSize) == NO)
   {
       CGFloat widthFactor = targetWidth / width;
       CGFloat heightFactor = targetHeight / height;
     
       if (widthFactor > heightFactor)
           scaleFactor = widthFactor; // scale to fit height
       else
           scaleFactor = heightFactor; // scale to fit width
       scaledWidth  = width * scaleFactor;
       scaledHeight = height * scaleFactor;
     
       // center the image
       if (widthFactor > heightFactor)
       {
           thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
       }
       else
           if (widthFactor < heightFactor)
           {
               thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
           }
   }     
 
   UIGraphicsBeginImageContext(targetSize); // this will crop
 
   CGRect thumbnailRect = CGRectZero;
   thumbnailRect.origin = thumbnailPoint;
   thumbnailRect.size.width  = scaledWidth;
   thumbnailRect.size.height = scaledHeight;
 
   [sourceImage drawInRect:thumbnailRect];
 
   newImage = UIGraphicsGetImageFromCurrentImageContext();
   if(newImage == nil)
       NSLog(@"could not scale image");
 
   //pop the context to get back to the default
   UIGraphicsEndImageContext();
   return newImage;
}
@end

- (void)aButton:(UIButton *)sender
{
   switch (sender.tag) {
       case 1:
           NSLog(@"Facebook");
           break;
       case 2:
           NSLog(@"Twitter");
           break;
       case 3:
           NSLog(@"Tumblr");
           break;
       case 4:
           NSLog(@"Plurk");
           break;
       default:
           break;
   }
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

   // Override point for customization after application launch.
   //Initialize the toolbar

   UIToolbar *toolbar = [[UIToolbar alloc] init];
   toolbar.barStyle = UIBarStyleDefault;
   toolbar.alpha = 0.8;
 
   //Set the toolbar to fit the width of the app.
   [toolbar sizeToFit];

   //Caclulate the height of the toolbar
   CGFloat toolbarHeight = [toolbar frame].size.height;

   //Get the bounds of the parent view
   CGRect rootViewBounds = [[UIScreen mainScreen] bounds];
 
   //Get the height of the parent view.
   CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
 
   //Get the width of the parent view,
   CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
 
   //Create a rectangle for the toolbar
   CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);
 
   //Reposition and resize the receiver
   [toolbar setFrame:rectArea];

   NSMutableArray *barButtonItemList = [[NSMutableArray alloc] init];
   CGFloat thumbWidth = 42 , thumbHeight = 42;
   {
       UIImage *image = [[UIImage imageNamed:@"facebook_icon.png"] imageByScalingAndCroppingForSize:CGSizeMake(thumbWidth, thumbHeight)];
       UIButton *button = [[UIButton alloc] init];
       [button setTag:1];
       [button setImage:image forState:UIControlStateNormal];
       [button setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
       [button addTarget:self action:@selector(aButton:) forControlEvents:UIControlEventTouchDown];
       UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
       [button release];
       [barButtonItemList addObject:barButton];
       [barButton release];
   }
   {
       UIImage *image = [[UIImage imageNamed:@"twitter_icon.png"] imageByScalingAndCroppingForSize:CGSizeMake(thumbWidth, thumbHeight)];
       UIButton *button = [[UIButton alloc] init];
       [button setTag:2];
       [button setImage:image forState:UIControlStateNormal];
       [button setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
       [button addTarget:self action:@selector(aButton:) forControlEvents:UIControlEventTouchDown];
       UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
       [button release];
       [barButtonItemList addObject:barButton];
       [barButton release];
   }
   {
       UIImage *image = [[UIImage imageNamed:@"tumblr_icon.png"] imageByScalingAndCroppingForSize:CGSizeMake(thumbWidth, thumbHeight)];
       UIButton *button = [[UIButton alloc] init];
       [button setTag:3];
       [button setImage:image forState:UIControlStateNormal];
       [button setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
       [button addTarget:self action:@selector(aButton:) forControlEvents:UIControlEventTouchDown];
       UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
       [button release];
       [barButtonItemList addObject:barButton];
       [barButton release];
   }
   {
       UIImage *image = [[UIImage imageNamed:@"plurk_icon.png"] imageByScalingAndCroppingForSize:CGSizeMake(thumbWidth, thumbHeight)];
       UIButton *button = [[UIButton alloc] init];
       [button setTag:4];
       [button setImage:image forState:UIControlStateNormal];
       [button setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
       [button addTarget:self action:@selector(aButton:) forControlEvents:UIControlEventTouchDown];
       UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
       [button release];
       [barButtonItemList addObject:barButton];
       [barButton release];
   }
   [toolbar setItems:barButtonItemList];
 
   [window addSubview:toolbar];
 
   [window makeKeyAndVisible];
 
   return YES;
}

沒有留言:

張貼留言