开发者

Interactivity on UIImageView

开发者 https://www.devze.com 2023-03-23 03:28 出处:网络
I am newbie working on iOS application for iPad. I have successfully implemented a children\'s book app using UIImageView. I have also implemented swipe gestures to traverse across the pages. Each pag

I am newbie working on iOS application for iPad. I have successfully implemented a children's book app using UIImageView. I have also implemented swipe gestures to traverse across the pages. Each page is loaded as single image inside UIImageView and plays an audio narrating the scene.

Now I would like to add some interactivity to my pages. For example, I have a single image loaded in UIImageView which contains few animal characters and some dialect f开发者_StackOverflow社区or that particular scene... I want whenever user tap on a particular animal, it should show some movement (may be as if lion is roaring) and my app should play a audio file containing that animal's sound... how can I achieve this functionality? I need to understand the concept so I can implement it. Should I be having separate images for all animals and load them in their own UIImageView and have all these UIImageViews inside main (or background) UIImageView? I would appreciate if someone can explain me the concept of how such functionality should be implemented.

I think for touches on animals I can define a list of CGRects that will act as hot-spots for each animal on the screen and then play sound files for touched animals but how can I define these hot-spots dynamically? because these would very on each page based on animal's position on a particular page/image. Also this might not help me with implementing some movement/animation for a particular animal.


An invisible button should work. Use a 1 pixel transparent png, and then size the button dynamically to fit the area you want tapped:

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(7, 5, 73, 30);
        [btn setImage:[UIImage imageNamed:@"btn_pixel.png"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(doSomething) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];

(you don't need to dealloc the button, because it was not alloc'ed in the first place)

Then, just create a function called "doSomething" that...does what you want. In your example, it would play the animal sound.


If the animals have pictures that are separate you should use them as UIButtons and make their picture "custom" and select your animal-pictures.

Otherwise maybe put an invisible button on-top of the animals and then do the code below, I think an .png with transparency with the right size and a custom UIButton should work!

To make these buttons do sounds, do:

-(IBAction)lionRoar {

NSString *path = [[NSBundle mainBundle]
                  pathForResource:@"LionRoar" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio play];


}

Hope that helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消