Some favorite site feeds aggregated locally: iPhone Development RSS   Adobe Labs RSS   Macrumors RSS

Tuesday, July 21, 2009

iPhone: Question: Setting lots of UIButtons?

Tuesday, July 21, 2009   

I am playing around with UIScrollView and one of the things I am doing is loading it up with a large image that one can pan around and zoom in and out of. That part is done and was easy. Now, I'd like to add hot spots to that image and the easiest way I've initially thought to do that is by setting custom buttons on top of it.

What I really wanted to do was to create a really wide view that contains my image and would allow me to place my UIButtons by hand on top, but when using IB for the iPhone, you can't stretch a View out wider than the screen. Even if I can do that with code when it loads up, I still can't place those buttons by hand. At least I don't think that one can. I haven't seen how, and it would be a really nice enhancement feature somehow if one could with a special View XIB or something.

Anyway, I have a bunch of hotspots that I need to place. Currently it's awfully tedious and it's being done one at a time by hand. I am wondering if I should write a method that takes x, y, width, height, and just loops through an array and places these one at a time in the method.

I am not exactly sure how I would set that up in Obj-C yet at the moment, but it must be close to AS3 in doing so. My current pseudo-code:
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"zooeyDeschanel" ofType:@"png"]];
imageView = [[UIImageView alloc] initWithImage:image];

myScrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
myScrollView.maximumZoomScale = 6.0;
myScrollView.minimumZoomScale = 1.0;
myScrollView.clipsToBounds = YES;
myScrollView.delegate = self;

UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
but.frame = CGRectMake( 0, 0, 50, 50 );
but.tag = 0;
[but setTitle:@"Test" forState:UIControlStateNormal];
[but addTarget:self action:@selector(displayValue:) forControlEvents:UIControlEventTouchUpInside];

UIButton *but2 = [UIButton buttonWithType:UIButtonTypeCustom];
but2.frame = CGRectMake( 200, 0, 50, 50 );
but2.tag = 1;
[but2 setTitle:@"Test 2" forState:UIControlStateNormal];
[but2 addTarget:self action:@selector(displayValue:) forControlEvents:UIControlEventTouchUpInside];

//... repeat this over and over

[myScrollView addSubview:imageView];
[myScrollView addSubview:but];
[myScrollView addSubview:but2];
I guess I am looking for a way to construct that loop. Probably shouldn't be too hard, but I wonder if there is a better way than floating the buttons above the image in the scroll view to begin with.

To be able to layout a really big view somehow would be a big plus instead of nailing down all the positions by hand.

Labels: ,

 
 Return to the main page
Comments:

There are currently 1 Comments:

Anonymous Anonymous said...
“If I had enough hotspots to make it worthwhile (six or more, maybe?) I'd just put that information into a configuration file of some sort and read it in at runtime and use the information to dynamically lay out the buttons. Especially when you think about how easy it would be to update the hotspots if for some reason your image ever changes.”
 
 Leave a comment