Can't get a UIScrollView to zoom and scroll - ios

I know this has been posted about a million times, but I cannot for the life of me get this Scrollview to work how it should. I have followed about four different tutorials and have spent the last two days attempting to fix this, so I really do hope someone can help me through this (if there happens to be another post that fixes my problem that I didn't see, I'm really sorry).
I have an Imageview inside of a Scrollview, containing a picture I'd like to be zoomable/scrollable when it's zoomed.
In my WDViewController.h file:
#import <UIKit/UIKit.h>
#interface WDViewController : UIViewController <UIScrollViewDelegate>
#property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
#property (nonatomic, retain) IBOutlet UIImageView *img_wd;
#end
And in my WDViewController.m file:
#import "WDViewController.h"
#implementation WDViewController
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.img_wd;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.minimumZoomScale=0.5;
self.scrollView.maximumZoomScale=6.0;
self.scrollView.delegate=self;
self.scrollView.contentSize=CGSizeMake(1280, 960);
self.scrollView.scrollEnabled = true;
self.scrollView.userInteractionEnabled = true;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Autolayout is disabled, as I noticed that caused problems to other users on Stack Overflow. I made sure that User Interaction and Multiple Touch were enabled in the Storyboard. But when I launch the app in a simulator, it just sits there. No matter how many times I pinch it doesn't zoom. I thought I did everything, but I must have missed a step that I'm just not seeing for some reason. Can anyone give me a hand?
EDIT: After the comments below, here's the code I have now (for WDViewController.m, .h hasn't changed):
#import "WDViewController.h"
#implementation WDViewController
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.img_wd;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.scrollView addSubview:self.img_wd];
self.img_wd.frame = CGRectMake(0, 0, 1280, 960);
self.scrollView.minimumZoomScale=0.5;
self.scrollView.maximumZoomScale=6.0;
self.scrollView.delegate=self;
self.scrollView.contentSize=CGSizeMake(1280, 960);
self.scrollView.scrollEnabled = true;
self.scrollView.userInteractionEnabled = true;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
EDIT: And after testing it on my phone, it works. Um... okay then, thanks everyone. My simulator just refuses to accept it. How odd.

Just a hunch:
To pinch in the iOS Simulator, you have to hold down the option key.

View hierarchy in IB:
The scroll view was dragged and dropped onto the root view and was snapped into place to fill the entire root view. Image view was dragged and dropped onto the scroll view and was snapped into place to fill the entire scroll view.
Attributes for image view in IB:
All source code:

did you add the image view to scrollview's subview? [self.scrollView addSubView:self.img_wd]

Related

Nested UIScrollViews. Trying to get one to zoom out with the same speed and factor as the other zooming in. View frozen

I am trying to set up nested UIScrollViews. What I have is two scroll views with the same content size 640*640 setup like this: canvasScrollView (a UIScrollView) > contentScrollView (a UIScrollView) > content (a UIView). These are all in a UIViewController. What I want to happen is, when the user pinches to zoom in on the content in contentScrollView the canvasScrollView view zooms out at with same factor and speed. I have tried setting up code like below:
#import "ViewController.h"
#import "ContentView.h"
#interface ViewController () <UIScrollViewDelegate>
#property (weak, nonatomic) IBOutlet UIScrollView *canvasScrollView;
#property (strong, nonatomic) UIView *viewForZoom;
#property (strong, nonatomic) ContentView *content;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
contentScrollView.contentSize=CGSizeMake(640 ,640);
contentScrollView.minimumZoomScale=0.25;
contentScrollView.maximumZoomScale=1;
self.content = [[[NSBundle mainBundle] loadNibNamed:#"floatingView" owner:self options:nil] objectAtIndex:0];
self.viewForZoom=self.content;
[contentScrollView addSubview:self.content];
contentScrollView.delegate=self;
//canvasScrollView was setup in the interface builder, same dimensions as above 320*568
self.canvasScrollView.contentSize=CGSizeMake(640 ,640);
self.canvasScrollView.minimumZoomScale=0.25;
self.canvasScrollView.maximumZoomScale=1;
self.canvasScrollView.userInteractionEnabled=NO;
[self.canvasScrollView addSubview:contentScrollView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.viewForZoom;
}
My first step is to make sure it is the contentScrollView that zooms and not the canvasScrollView. With my code above the views load up fine but I am unable to pan or zoom the content. I'm not sure why this doesn't work. I have disabled the canvasScrollView from user interaction as later on this will be modified programmatically, I have also set the contentScrollView delegate. Not working though. Would really appreciate some help on this one!!!
Thanks
You have added contentScrollView to a view that has userInteractionEnabled=NO. A view that doesn't allow user interaction does not allow user interaction any of its subviews. Try attaching it to the self.view instead.
[self.view addSubview:contentScrollView];

Image Not Showing Up on UIScrollView

So I have a UIScrollView and scrolling works. I added a label on it and it scrolled that too. When I added an Image View, however, scrolling works but the image does not show up and I get this error:
Could not load the "block" image referenced from a nib in the bundle with identifier
My ViewController.m:
#interface ViewController : UIViewController{
IBOutlet UIScrollView * scroller;
}
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 600)];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end
I have disabled "Use Auto Layout," Changed the Size to "Freeform," and changed the views height to 600.
Any ideas on how I can make this work?

UIScrollView doesn’t seem to work in xcode 6 (iOS 8)

I’m trying to make a scroll view in xcode. I used the following code in xcode 5 (which worked):
view.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UIScrollView *ScrollView;
}
#end
view.m
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[ScrollView setScrollEnabled:YES];
[ScrollView setContentSize:CGSizeMake(320, 1005)];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I have seen others with this problem but none of their solutions worked for me. Note that i have size set to freedom and auto layout is turned off.
Try this - UIScrollView with Autolayout
OR
try setting your content size in viewDidAppear
Due to a scrollView feature in auto layout, the contentSize is being redefined. To solve this problem, you could disable auto layout or set the content size in viewDidLayoutSubviews()
-(void)viewWillLayoutSubviews {
[super viewDidLayoutSubviews];
ScrollView.contentSize = CGSizeMake(320,1005);
}
So you would want to add this code and delete the ScrollView.contentSize method in viewDidLoad.
I don't see anything immediately wrong with this code. I would check and makes sure that you've connected the IBOutlet properly in the Interface Builder.

Why can't I access UIImageview.center?

I'm working on an old project of mine with a xib file in it. I have a button that changes the position of an UIImageview to 0,0, but it doesn't work and the label stays at its initial position. When I create new project with the exact same code expect it contains a mainStoryboard.Storyboard it works fine. I suppose it has something to do with the xib file and files owner.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)Action1:(id)sender {
imageview.center = CGPointMake(0, 0);
}
If you're using auto layout, changing the frame may not have any effect.
Instead, you need to change the constraints on the view.
You can find a written tutorial on auto layout on Ray Wenderlich's site here or a video tutorial here.

Handling touches within UIScrollview

I have a view that contains a UIScrollView. The UIScrollView contains an UIImageView (only one for now but there will be more latter). I want to be able to drag the UIImageView out of the UIScrollView and into the superview.
I have reviewed every similar question and example I can find online and still cant get my head around how this works.
At first I couldn't get touches to register all whithin the scrollview. After a lot of searching I have found a way to get touchesbegan to register as follows:
First I created a new view based project and in the auto-created "ViewController.h"
I created an IBOutlet named slider.
Then in the auto-created "ViewController.m" I created the UIScrollview.
In IB I added a UIScrollView to the storyboard then connected it to the "slider" IBOutlet.
Next I added class file to the projects. This new class is a subclass os UIImageView. I named this class "DragableImage" In the
Then in IB I dropped an image into the UIScrollview clicked on it and changed the class to "DragableImage"
In the DragableImage.m I voided touches began and simply told it to become hidden if clicked.
This works and the touch registers and makes the image disappear but how do I now add the new image to the main view and have it follow my finger?
This is what I have in the way of code:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UIScrollView *slider;
}
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[slider setScrollEnabled:YES];
[slider setContentSize:CGSizeMake(306, 1560)];
[slider setShowsVerticalScrollIndicator:NO];
slider.canCancelContentTouches = NO;
slider.delaysContentTouches = NO;
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#end
DragableImage.h
#import <UIKit/UIKit.h>
#interface DragableImage : UIImageView
#end
DragableImage.m
#implementation DragableImage
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.hidden = YES;
}
#end
How to make this work?
There is one obvious problem that can arise here. Namely, how to tell the difference between dragging to scroll and dragging an image. The way I would handle this is to setup a tap and hold gesture recognizer (http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizers/GestureRecognizers.html).
Once you detect a hold you could set it up so that further touches/gestures get routed to you drag and drop code. You can find a nice tutorial for that here:
http://www.ancientprogramming.com/2012/04/05/drag-and-drop-between-multiple-uiviews-in-ios/

Resources