I have a viewcontroller that is set to scrollview delegate (the scrollview is a subview of the viewcontroller), when I put [scrollview removeFromSuperview]; both the viewcontroller and the scrollview get removed and I'm just left with an empty window.
How can I remove only the scrollview?
edit::::
this is part of my viewcontroller .h
#interface QuartzViewController : UIViewController <UIScrollViewDelegate> {
this is part of my viewcontroller .m
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
UIInterfaceOrientation o = self.interfaceOrientation;
if ((o == UIInterfaceOrientationPortrait) || (o == UIInterfaceOrientationPortraitUpsideDown)) {D = 1;pagingScrollView.delegate = nil;[pagingScrollView removeFromSuperview];[self resurrectPaging];}
if ((o == UIInterfaceOrientationLandscapeLeft) || (o == UIInterfaceOrientationLandscapeRight)) {D = 2;pagingScrollView.delegate = nil;[pagingScrollView removeFromSuperview];[self resurrectPaging];}}
-(void)resurrectPaging {
CGRect F;
F = [self frameForPagingScrollView];
pagingScrollView = [[UIScrollView alloc] initWithFrame:F];
[SELF addSubview:pagingScrollView];
[self setPaging];}
- (void)viewDidLoad {
[self.view addSubview:SELF];
D = 1;
CGRect F;
F = [self frameForPagingScrollView];
pagingScrollView = [[UIScrollView alloc] initWithFrame:F];
[self setPaging];}
- (void)setPaging {
if (D == 1) {
CGRect F;
F = [self frameForPagingScrollView];
pagingScrollView.pagingEnabled = YES;
pagingScrollView.backgroundColor = [UIColor whiteColor];
pagingScrollView.showsVerticalScrollIndicator = NO;
pagingScrollView.showsHorizontalScrollIndicator = NO;
pagingScrollView.contentSize = CGSizeMake(F.size.width * [self pdfPageCount], F.size.height);
pagingScrollView.delegate = self;
pagingScrollView.clipsToBounds = YES;
pagingScrollView.bounces = YES;
self.view = pagingScrollView;
recycledPages = [[NSMutableSet alloc] init];
visiblePages = [[NSMutableSet alloc] init];
[self tilePages];
[pagingScrollView addSubview:self.isvP];}
else if (D == 2) {
CGRect F;
F = [self frameForPagingScrollView];
pagingScrollView.pagingEnabled = YES;
pagingScrollView.backgroundColor = [UIColor redColor];
pagingScrollView.showsVerticalScrollIndicator = NO;
pagingScrollView.showsHorizontalScrollIndicator = NO;
pagingScrollView.contentSize = CGSizeMake(F.size.width * [self pdfPageCount], F.size.height);
pagingScrollView.delegate = self;
pagingScrollView.clipsToBounds = YES;
pagingScrollView.bounces = YES;
self.view = pagingScrollView;
recycledPages = [[NSMutableSet alloc] init];
visiblePages = [[NSMutableSet alloc] init];
[self tilePages];
[pagingScrollView addSubview:self.isvL];}}
basically the reason i what to remove then add my scrollview again is because when the device orientation changes to landscape my landscape subview is in the wrong place (x, y).
but say if i start off in landscape isvL is in the right place so I know I got the code correct. going to landscape screws the x, y positions of isvL in my pagingScrollView!
basically the reason i what to remove then add my scrollview again is because when the device orientation changes to landscape my landscape subview is in the wrong place (x, y).
If you having trouble with the positioning of your scroll view, you should try setting autoresize masks correctly, or manually adjusting the frame. There is no reason to remove and recreate a scroll view every time.
Also, in the code you have listed, you appear to be leaking your scroll view.
Related
I am creating the simple customized camera application.
so. i tried to customize the buttons for flash setting button and capture.
It worked for iPhone4 and iPhone4s... but unfortunately.. it shows the white screen on iPhone3gs.
What's the problem?
#define CAMERA_TRANSFORM_X 1
#define CAMERA_TRANSFORM_Y 1
#define SCREEN_WIDTH 320
#define SCREEN_HEIGTH 480
- (void) showCamera
{
overlay = [[CustomOverlayView alloc]
initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH)];
overlay.delegate = self;
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.navigationBarHidden = YES;
self.picker.toolbarHidden = YES;
self.picker.wantsFullScreenLayout = YES;
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.showsCameraControls = NO;
self.picker.cameraViewTransform = CGAffineTransformScale(self.picker.cameraViewTransform, CAMERA_TRANSFORM_X, CAMERA_TRANSFORM_Y);
self.picker.cameraOverlayView = overlay;
[self.view addsubview self.picker.view];
}
Call your showCamera method in your root view controller viewDidLoad method, or in its init method if you don't use IB.
You should present your camera controller by using
[self.navigationController presentModalViewContoller:self.picker animated:NO];
#implementation MainViewController
#synthesize scrollView,imageView;
- (id) init {
if (self != nil) {
self.title = #"Evolution";
}
return self;}
- (void)viewDidLoad {
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
int numberOfImages = 32;
CGFloat currentX = 0.0f;
for (int i=1; i <= numberOfImages; i++) {
// create image
NSString *imageName = [NSString stringWithFormat:#"page-%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
imageView = [[UIImageView alloc] initWithImage:image];
// put image on correct position
CGRect rect = imageView.frame;
rect.origin.x = currentX;
imageView.frame = rect;
// update currentX
currentX +=454; //mageView.frame.size.width;
[scrollView addSubview:imageView];
[imageView release];
}
[scrollView addGestureRecognizer:tapGesture];
scrollView.contentSize = CGSizeMake(currentX, 800);
scrollView.pagingEnabled=YES;
scrollView.userInteractionEnabled = YES;
scrollView.maximumZoomScale = 15;
scrollView.minimumZoomScale = 0.5;
scrollView.bounces = NO;
scrollView.bouncesZoom = NO;
scrollView.delegate = self;
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[self.view addSubview:scrollView];
[scrollView release];
[super viewDidLoad];}
- (BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)interfaceOrientation{
return YES;}
-(void)tapDetected:(UIGestureRecognizer*)recognizer{
ImageViewController *settings = [[ImageViewController alloc] init];
[self.navigationController pushViewController:settings animated:YES];
[settings release];}
#end
#implementation ImgScrollViewAppDelegate
#synthesize window;#synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application{
LogMethod();
// If you want the status bar to be hidden at launch use this:
// application.statusBarHidden = YES;
//
// To set the status bar as black, use the following:
// application.statusBarStyle = UIStatusBarStyleBlackOpaque;
// Create window
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// this helps in debugging, so that you know "exactly" where your views are placed;
// if you see "red", you are looking at the bare window, otherwise use black
// window.backgroundColor = [UIColor redColor];
viewController = [ [ MainViewController alloc ] init ];
navigationController = [ [ UINavigationController alloc ] initWithRootViewController: viewController ];
/* Anchor the view to the window */
[window addSubview:[navigationController view]];
/* Make the window key and visible */
[window makeKeyAndVisible];
}
#end
in above code i have problem that is when check it on simulator then rotate device left and right. But view not change and i want to change it. how i modify it?
In your view controller implementation there is a method - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation. If you want it to rotate to any orientation, make it return YES. If you only want it to autorotate to some orientations, add an if statement to check and return yes or no.
I try to make following: Horizontal list of images, that I can select and do something with it. For example flip image around x axis. I know how to rotate image. I created scrollview and load it with images. I added event handler when I tap on image. But I don't know how to do something with tapped image. How to code method to do something with tapped image?
- (void)viewDidLoad {
[super viewDidLoad];
img1 = [UIImage imageNamed:#"imgTest.jpg"];
img2 = [UIImage imageNamed:#"imgTest2.jpg"];
arrayOfImages = [[NSMutableArray alloc] init];
[arrayOfImages addObject:img1];
[arrayOfImages addObject:img2];
[arrayOfImages addObject:img1];
[arrayOfImages addObject:img2];
scrollView = [[UIScrollView alloc] init];
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
scrollView.directionalLockEnabled = YES;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.delegate = self;
scrollView.backgroundColor = [UIColor blueColor];
scrollView.autoresizesSubviews = YES;
scrollView.frame = CGRectMake(0, 0, 320, 128);
[self.view addSubview:scrollView];
UIImage *imageToAdd;
int x = 0;
int y = 0;
for(imageToAdd in arrayOfImages)
{
UIImageView *temp = [[UIImageView alloc] initWithImage:imageToAdd];
temp.frame = CGRectMake(x, y, 128, 128);
temp.userInteractionEnabled = YES;
x += 135;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[temp addGestureRecognizer:tap];
[scrollView addSubview:temp];
}
...
- (void)imageTapped:(UIGestureRecognizer *)sender
{
// how to get reference on selected item in scrollview???
}
A gesture recognizer has a view property that returns the view associated with the recognizer. Since you know that it'll be a UIImageView you can simply cast it and use it as in:
UIImageView *iv = (UIImageView *)[sender view];
And your image can then be queried via:
UIImage *image = [iv image];
If you need to know the index in your array, there are two ways: either simply use [arrayOfImages indexOfObject:image];, or you can assign tags (numbers) to views and use them. A tag is not used by Apple, it's only here so we developers can "mark" views in some way. For example:
NSInteger counter = 0;
for(imageToAdd in arrayOfImages)
{
UIImageView *temp = [[UIImageView alloc] initWithImage:imageToAdd];
count.tag = counter++;
...
}
Then, in your imageTapped:, you can query the index via tag:
NSInteger index = [imageView tag];
Hi I have populated uiscrollview with images (code is bellow) but I don't know how to add some event to images inside. I want to double tap on image inside scrollview and get some event. Any direction how to achieve this?
arrayOfImages = [[NSMutableArray alloc] init];
NSString *img;
for (img in imgArray)
{
[arrayOfImages addObject:[UIImage imageNamed:img]];
}
NSLog(#"Array initialization complete...");
scrollView = [[UIScrollView alloc] init];
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
scrollView.directionalLockEnabled = YES;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.delegate = self;
scrollView.backgroundColor = [UIColor blueColor];
scrollView.autoresizesSubviews = YES;
scrollView.frame = CGRectMake(0, 0, 320, 29);
[self.view addSubview:scrollView];
NSLog(#"Scroll View initialization setup complete...");
UIImage *imageToAdd;
int x = 0;
int y = 0;
for (imageToAdd in arrayOfImages)
{
UIImageView *temp = [[UIImageView alloc] initWithImage:imageToAdd];
temp.frame = CGRectMake(x, y, 29, 29);
x += 29;
[scrollView addSubview:temp];
}
NSLog(#"Adding images to outlet complete...");
Add a UITapGestureRecognizer to the UIImageView(s).
Look at this guide for more info.
I want pass a number from my ViewController to the the TweetViewController. Everything worked okay, I did it with NSUInteger as property (randomNumber and tweetNumber):
TweetViewController *Second = [[TweetViewController alloc] initWithNibName:nil bundle:nil];
Second.tweetNumber = randomNumber;
Second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:Second animated:YES];
[Second release];
I have now changed my code because I did not want that only my TweetViewVontroller is loaded. I wanted a homescreenlike swipe between the Tweet- and InfoViewController. I use a SwitchViewController, which will be load instead of the TweetViewController. The SwitchViewController looks like this (window1/2 are UIViewController):
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320,460)];
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0,0,640,460)];
self.window1 = [TweetViewController alloc];
self.window2 = [InfoViewController alloc];
[contentView addSubview:self.window1.view];
CGRect f = self.window2.view.frame;
f.origin.x = 320;
self.window2.view.frame = f;
[contentView addSubview:self.window2.view];
[scrollView addSubview:contentView];
scrollView.contentSize = contentView.frame.size;
scrollView.pagingEnabled = YES;
self.view = scrollView;
[contentView release];
[scrollView release];
Now I can´t pass the number from the FirstViewController to the TweetViewController. Any Idea, how to solve the problem?
window1.tweetNumber = randomNumber;
If you want to update this every time the scrollview is scrolled, you might want to look into the UIScrollViewDelegate protocol. Specifically scrollViewWillBeginDragging: