I'm trying to make a simple app on iPad. For now I only display full screen pictures in a custom view. My problem is that my view doesn't autoSized when the iPad rotates...the rotation works perfect but the size of my images are still the same.
My application is based on this because I wanted this effect when I switch pictures. Basically, the view that make the effect is FlipView
that inherit form GenericAnimationView
which inherit form UIView
. There's also AnimationFrame
that inherit from NSObject
which is required to display 1 animation cycle. Finally, there's AnimationDelegate
that inherit from NSObject
which handle callbacks from transform operations.
Here is my AnimationViewController
#import "AnimationViewController.h"
#import "FlipView.h"
#import "AnimationDelegate.h"
@implementation AnimationViewController
@synthesize flipView2;
@synthesize panRecognizer;
@synthesize panRegion;
@synthesize imageBluehound,imageDoggie,imagePointy,imagePurpGuy,imageRedDog,imageWoof;
- (id)init
{
self = [super init];
if (self) {
// Custom initialization
step = 0;
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
- (void)dealloc
{
[flipView2 release];
[panRecognizer release];
[panRegion release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onBackButtonPressed:)];
// memorisation des imagesx
imageBluehound = [UIImage imageNamed:@"Bluehound.gif"];
imageDoggie = [UIImage imageNamed:@"Doggie.gif"];
imagePointy = [UIImage imageNamed:@"Pointy.gif"];
imagePurpGuy = [UIImage imageNamed:@"PurpGuy.gif"];
imageRedDog = [UIImage imageNamed:@"RedDog.gif"];
imageWoof = [UIImage imageNamed:@"Woof.gif"];
animationDelegate2 = [[AnimationDelegate alloc] initWithSequenceType:kSequenceControlled
directionType:kDirectionForward];
animationDelegate2.controller = self;
animationDelegate2.perspectiveDepth = 1000;
self.flipView2 = [[FlipView alloc] initWithAnimationType:kAnimationFlipHorizontal
frame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
animationDelegate2.transformView = flipView2;
[self.view addSubview:flipView2];
[flipView2 printText:@"" usingImage:imageBluehound backgroundColor:[UIColor greenColor] textColor:nil];
[flipView2 printText:@"" usingImage:imageDoggie backgroundColor:[UIColor greenColor] textColor:nil];
[flipView2 printText:@"" usingImage:imagePointy backgroundColor:[UIColor greenColor] textColor:nil];
[flipView2 printText:@"" usingImage:imagePurpGuy backgroundColor:[UIColor greenColor] textColor:nil];
[flipView2 printText:@"" usingImage:imageRedDog backgroundColor:[UIColor greenColor] textColor:nil];
[flipView2 printText:@"" usingImage:imageWoof backgroundColor:[UIColor greenColor] textColor:nil];
self.panRegion = [[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
[self.view addSubview:panRegion];
self.panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panned:)];
panRecognizer.delegate = self;
panRecognizer.maximumNumberOfTouches = 1;
panRecognizer.minimumNumberOfTouches = 1;
[self.view addGestureRecognizer:panRecognizer];
}
- (void)onBackButtonPressed:(UIBarButtonItem *)sender
{
[self dismissModalViewControllerAnimated:YES];
}
- (void)panned:(UIPanGestureRecognizer *)recognizer
{
switch (recognizer.state) {
case UIGestureRecognizerStatePossible:
break;
// case UIGestureRecognizerStateRecognized: // for discrete recognizers
// break;
case UIGestureRecognizerStateFailed: // cannot recognize for multi touch sequence
break;
case UIGestureRecognizerStateBegan: {
// allow controlled flip only when touch begins within the pan region
if (CGRectContainsPoint(panRegion.frame, [recognizer locationInView:self.view])) {
if (animationDelegate2.animationState == 0) {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
animationDelegate2.sequenceType = kSequenceControlled;
animationDelegate2.animationLock = YES;
}
}
}
break;
case UIGestureRecognizerStateChanged: {
if (animationDelegate2.animationLock) {
switch (flipView2.animationType) {
case kAnimationFlipVertical: {
float value = [recognizer translationInView:self.view].y;
[animationDelegate2 setTransformValue:value delegating:NO];
}
break;
case kAnimationFlipHorizontal: {
float value = [recognizer translationInView:self.view].x;
[animationDelegate2 setTransformValue:value delegating:NO];
}
break;
default:break;
}
}
}
break;
case UIGestureRecognizerStateCancelled: // cancellation touch
break;
case UIGestureRecognizerStateEnded: {
if (animationDelegate2.animationLock) {
// provide inertia to panning gesture
float value = sqrtf(fabsf([recognizer velocityInView:self.view].x))/10.0f;
[animationDelegate2 endStateWithSpeed:value];
}
}
break;
default:
break;
}
}
// use this to trigger events after specific interactions
- (void)animationDidFinish:(int)direction
{
switch (step) {
case 0:
break;
case 1:
break;
default:break;
}
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationIsLandscape(orientation))
{
if (orientation == UIInterfaceOrientationLandscapeLeft)
{
NSLog(@"landscape left");
self.flipView2.transform = CGAffineTransformMakeRotation(0);
}
else
{
NSLog(@"landscape right");
self.flipView2.transform = CGAffineTransformMakeRotation(0);
}
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
@end
And here the .h
#import <UIKit/UIKit.h>
@class FlipView;
@class AnimationDelegate;
@interface AnimationViewController : UIViewController <UIGestureRecognizerDelegate> {
// use this to choreograph a sequence of animations that you want the user to step through
int step;
//the controller needs a reference to the delegate for control of the animation sequence
AnimationDelegate *animationDelegate2;
BOOL runWhenRestart;
}
//@property (nonatomic, retain) UIButton *boutonMenuGlissant;
@property (nonatomic, retain) UIImage *imageBluehound;
@property (nonatomic, retain) UIImage *imageDoggie;
@property (nonatomic, retain) UIImage *imagePointy;
@property (nonatomic, retain) UIImage *imagePurpGuy;
@property (nonatomic, retain) UIImage *imageRedDog;
@property (nonatomic, retain) UIImage *imageWoof;
@property (nonatomic, retain) FlipView *flipView2;
@property (nonatomic, retain) UIView *panRegion;
@property (nonatomic, retain) UIPanGestureRecognizer *panRecognizer;
- (void)onBackButtonPressed:(UIBarButtonItem *)sender;
- (void)panned:(UIPanGestureRecognizer *)recognizer;
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration;
// animation delegate will notify the controller when the animation frame has reached a position of rest
- (void)animationDidFinish:(int)direction;
@end
I saw in other posts that they talked about the autoSized mask but I couldn't make it...
Any idea or suggestion? Thanks!
Try modifying the struts and strings of your views/images. After clicking on the image, go to the size tab of the utility inspector. The example next to "autosizing" will show what the view will look like as the frame changes. Turn on/off the red connectors and arrows to make the image look the way you want when the orientation changes.