Xcode版本4.5.1
1. 開啓一個專案
2. 在storyBoard上加上一個button
3. 加入一個背景所需要的圖檔
4. 在mainViewController.h 註冊button
#import <UIKit/UIKit.h>
@interface mainViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *changeButton;
@end
5. 在mainViewController.m加入六種背景圖的設定程式碼。此六種會由按鍵控制,而輪流出現。
#import "mainViewController.h"
@interface mainViewController ()
@end
@implementation mainViewController
@synthesize changeButton;
int caseNum=0;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)changeBackground:(id)sender {
caseNum++;
switch (caseNum) {
case 1:
[self setBackgroundImage1];
[changeButton setTitle:@"改變背景:第一種設定" forState:UIControlStateNormal];
break;
case 2:
[self clearBackgroundImage1];
[changeButton setTitle:@"改變背景:第一種清除" forState:UIControlStateNormal];
break;
case 3:
[self setBackgroundImage2];
[changeButton setTitle:@"改變背景:第二種設定" forState:UIControlStateNormal];
break;
case 4:
[self clearBackgroundImage2];
[changeButton setTitle:@"改變背景:第二種清除" forState:UIControlStateNormal];
break;
case 5:
[self setBackgroundImage3];
[changeButton setTitle:@"改變背景:第三種設定" forState:UIControlStateNormal];
break;
case 6:
[self clearBackgroundImage3];
[changeButton setTitle:@"改變背景:第三種清除" forState:UIControlStateNormal];
break;
case 7:
[self setBackgroundImage4];
[changeButton setTitle:@"改變背景:第四種設定" forState:UIControlStateNormal];
break;
case 8:
[self clearBackgroundImage4];
[changeButton setTitle:@"改變背景:第四種清除" forState:UIControlStateNormal];
break;
case 9:
[self setBackgroundImage5];
[changeButton setTitle:@"改變背景:第五種設定" forState:UIControlStateNormal];
break;
case 10:
[self clearBackgroundImage5];
[changeButton setTitle:@"改變背景:第五種清除" forState:UIControlStateNormal];
break;
case 11:
[self setBackgroundImage6];
[changeButton setTitle:@"改變背景:第六種設定" forState:UIControlStateNormal];
break;
case 12:
[self clearBackgroundImage6];
[changeButton setTitle:@"改變背景:第六種清除" forState:UIControlStateNormal];
caseNum = 0;
break;
default:
break;
}
}
- (void) setBackgroundImage1
{
// 水平、垂直並排(repeat)
UIColor *bgColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"TaiwanMap.jpg"]];
self.view.backgroundColor = bgColor;
}
- (void) clearBackgroundImage1
{
self.view.backgroundColor = [UIColor clearColor];
}
- (void) setBackgroundImage2
{
// 水平、垂直並排(repeat)
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"TaiwanMap.jpg"]];
}
- (void) clearBackgroundImage2
{
self.view.backgroundColor = [UIColor clearColor];
}
- (void) setBackgroundImage3
{
UIImage *img = [[UIImage alloc]initWithContentsOfFile:
[[NSBundle mainBundle]pathForResource:@"TaiwanMap" ofType:@"jpg"]];
[self.view setBackgroundColor:[UIColor colorWithPatternImage:img]];
}
- (void) clearBackgroundImage3
{
self.view.backgroundColor = [UIColor clearColor];
}
- (void) setBackgroundImage4
{
// 滿版的背景圖(圖片會被撐大)
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:@"TaiwanMap.jpg"] drawInRect:self.view.bounds];
UIImage *backgroundImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];
}
- (void) clearBackgroundImage4
{
self.view.backgroundColor = [UIColor clearColor];
}
- (void) setBackgroundImage5
{
//滿版的背景圖(圖片會被撐大)
UIImageView *contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[contentView setImage:[UIImage imageNamed:@"TaiwanMap.jpg"]];
contentView.frame = CGRectMake(0, 0, 1024, 768); //設定填滿螢幕大小
[self.view addSubview:contentView];
[self.view sendSubviewToBack:contentView];
//self.view = contentView;
}
- (void) clearBackgroundImage5
{
for(UIView *subview in [self.view subviews]) {
if([subview isKindOfClass:[UIImageView class]]) {
[subview removeFromSuperview];
} else {
// Do nothing - not a UIButton or subclass instance
}
}
}
- (void) setBackgroundImage6 {
//貼入一張圖到UIImageView,再轉到 self.view ,最後再設定到最後面當背景圖,此法只會顯示一張不被放大的圖。
UIImageView *customBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TaiwanMap.jpg"]];
[self.view addSubview:customBackground];
[self.view sendSubviewToBack:customBackground];
}
- (void) clearBackgroundImage6
{
for(UIView *subview in [self.view subviews]) {
if([subview isKindOfClass:[UIImageView class]]) {
[subview removeFromSuperview];
} else {
// Do nothing - not a UIButton or subclass instance
}
}
}
@end
6, 執行結果
















2 則留言:
試問 ios ipad的背景架構可以做成透明的嗎
類似書報攤的背景!?
可以,將Layer設成透明就好了。
example
Layer.opacity = 0.1;(0~1)
張貼留言