04-圖片排列切換,04-圖片排列

來源:互聯網
上載者:User

04-圖片排列切換,04-圖片排列

ViewController.h檔案中:

1 @interface ViewController : UIViewController2 3 @property (weak, nonatomic) IBOutlet UISegmentedControl *segmentCtrl;4 5 - (IBAction)indexChange:(UISegmentedControl *)sender;6 7 @end

ViewController.m檔案中:

  1 #import "ViewController.h"  2   3 #define kImgWH 40  4 #define kExistCtrl 2 // 新增ImageView控制項之前已存在的控制項數目  5   6 @interface ViewController ()  7 {  8     int _imgCount;  // 圖片控制項的個數  9     UIButton *_btn; 10 } 11  12 @end 13  14 @implementation ViewController 15  16 - (void)viewDidLoad { 17     [super viewDidLoad]; 18  19     //初始化圖片控制項個數 20     _imgCount = 9; 21      22     // 先新增按鈕控制項(新增順序不可調換) 23     _btn = [[UIButton alloc] init]; 24     [_btn setTitle:@"新增" forState:UIControlStateNormal]; 25     [_btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 26     [self.view addSubview:_btn]; 27     // 監聽事件 28     [_btn addTarget:self action:@selector(addImg) forControlEvents:UIControlEventTouchUpInside]; 29      30     // 再新增圖片控制項 31     [self adjustImagePosWithColumns:2]; 32 } 33  34 #pragma mark 調整圖片位置的封裝函數 35 - (void)adjustImagePosWithColumns:(int)columns 36 { 37     CGFloat marginW = (self.view.frame.size.width - columns * kImgWH)/(columns + 1); 38     CGFloat marginH = 20; 39     CGFloat oneY = 80; 40      41     // 調整圖片或新增圖片控制項 42     for (int i = 0; i < _imgCount; ++i) { 43         int col = i % columns; 44         int row = i / columns; 45          46         int x = marginW + col * (marginW + kImgWH); 47         int y = oneY + row * (marginH + kImgWH); 48          49         CGRect frm = CGRectMake(x, y, kImgWH, kImgWH); 50          51         if (self.view.subviews.count < _imgCount + kExistCtrl) { 52             //動態建立UIImageView控制項 53             int no = i % 9; 54             NSString *name = [NSString stringWithFormat:@"01%d.png", no]; 55              56             UIImageView *img = [[UIImageView alloc] init]; 57             img.image = [UIImage imageNamed:name]; 58             img.frame = frm; 59              60             [self.view addSubview:img]; 61         } 62         else { 63             // 調整圖片的位置 64             UIView *child = self.view.subviews[i + kExistCtrl]; 65             child.frame = frm; 66         } 67     } 68      69     // 調整按鈕的位置 70     int col = _imgCount % columns; 71     int row = _imgCount / columns; 72     int x = marginW + col * (marginW + kImgWH); 73     int y = oneY + row * (marginH + kImgWH); 74      75     CGRect frm = CGRectMake(x, y, kImgWH, kImgWH); 76     _btn.frame = frm; 77      78 //    NSLog(@"子控制項個數=%lu", (unsigned long)self.view.subviews.count); 79 } 80  81 #pragma mark Segmented控制項的值變化的監聽事件 82 - (IBAction)indexChange:(UISegmentedControl *)sender { 83      84     [UIView beginAnimations:nil context:nil]; 85     [UIView setAnimationDuration:0.5]; 86      87     int columns = (int)(sender.selectedSegmentIndex + 2); 88     [self adjustImagePosWithColumns:columns]; 89      90     [UIView commitAnimations]; 91 } 92  93 #pragma mark “新增”按鈕的響應事件 94 - (void)addImg 95 { 96     // 新增圖片控制項 97     int no = _imgCount % 9; 98     NSString *name = [NSString stringWithFormat:@"01%d.png", no]; 99     100     UIImageView *img = [[UIImageView alloc] init];101     img.image = [UIImage imageNamed:name];102     img.frame = CGRectMake(_btn.frame.origin.x, _btn.frame.origin.y, 0, 0);103 //    img.frame = CGRectMake(0, 0, kImgWH, kImgWH);104     [self.view addSubview:img];105 106     [UIView beginAnimations:nil context:nil];107     [UIView setAnimationDuration:0.5];108     109     _imgCount += 1;110     int columns = (int)(self.segmentCtrl.selectedSegmentIndex + 2);111     [self adjustImagePosWithColumns:columns];112     113     [UIView commitAnimations];114 }115 116  117 @end

介面效果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.