iOS scrollview视窗滑动

来源:互联网 发布:淘宝客连接转换二维码 编辑:程序博客网 时间:2024/04/28 22:26
#import "ViewController.h"#import "testmenuEntity.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        _scrollview.backgroundColor = [UIColor blueColor];    _scrollview2.backgroundColor = [UIColor yellowColor];//    _scrollview.}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.        }-(void) viewDidAppear:(BOOL)animated{    _scrollview.contentSize = CGSizeMake(190, 1200);    _scrollview2.contentSize = CGSizeMake(150, 1000);}- (IBAction)addButton:(id)sender {    //    NSArray//    NSMutableArray    NSMutableArray *allmenu = [[NSMutableArray alloc] init];        int i = 0;    for(i = 0; i < 10; i++)    {                testmenuEntity *e = [[testmenuEntity alloc] init];        e.mid = 10;        e.mprice = 100.5;        e.mname = [NSString stringWithFormat:@"%@", @"菜类型1" ];                [allmenu addObject:e];                        testmenuEntity *e2 = [[testmenuEntity alloc] init];        e2.mid = 11;        e2.mprice = 100.5;        e2.mname = [NSString stringWithFormat:@"%@", @"菜类型2" ];                [allmenu addObject:e2];    }        for(i = 0 ; i < [allmenu count]; i++)    {        testmenuEntity *tmpe = [allmenu objectAtIndex:i];                //动态添加一个按钮        CGRect frame = CGRectMake(0, 0+55*i, 300, 50);        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];        button.frame = frame;                NSString *xxx = [NSString stringWithFormat:@"按钮:%d, %@", tmpe.mid, tmpe.mname];        [button setTitle:xxx forState: UIControlStateNormal];        button.backgroundColor = [UIColor redColor];        button.tag = tmpe.mid;        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];//        [self.view addSubview:button];        [_scrollview addSubview:button];    }}//这个是新按钮的响应函数-(IBAction) buttonClicked:(id)sender {        NSLog(@"11111");        NSMutableArray *allmenu = [[NSMutableArray alloc] init];        int i = 0;    for(i = 0; i < 2; i++)    {                testmenuEntity *e = [[testmenuEntity alloc] init];        e.mid = 10;        e.mprice = 100.5;        e.mname = [NSString stringWithFormat:@"%@", @"菜类型1" ];                [allmenu addObject:e];                        testmenuEntity *e2 = [[testmenuEntity alloc] init];        e2.mid = 11;        e2.mprice = 100.5;        e2.mname = [NSString stringWithFormat:@"%@", @"菜类型2" ];                [allmenu addObject:e2];    }        for(i = 0 ; i < [allmenu count]; i++)    {        testmenuEntity *tmpe = [allmenu objectAtIndex:i];                //动态添加一个按钮        CGRect frame = CGRectMake(0, 0+55*i, 300, 50);        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];        button.frame = frame;                NSString *xxx = [NSString stringWithFormat:@"按钮:%d, %@", tmpe.mid, tmpe.mname];        [button setTitle:xxx forState: UIControlStateNormal];        button.backgroundColor = [UIColor redColor];        button.tag = tmpe.mid;        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];        //        [self.view addSubview:button];        [_scrollview2 addSubview:button];    }}@end


0 0