Sliver challenge question


#1

I have been working with this for days. But I’m not able to zoom the view. I can’t see any mistake in the code. Any help will be appreciated.


#import "BNRAppDelegate.h"
#import "BNRHypnosisView.h"

@interface BNRAppDelegate () <UIScrollViewDelegate>

@property (nonatomic,strong) BNRHypnosisView *hv;

@end

@implementation BNRAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    // Create a CGRect that will be the frame
    CGRect screenRect = self.window.bounds;
    CGRect bigRect = screenRect;
    bigRect.size.width *= 2.0;
    
    // Create a screen-sized scroll view and add it to the window
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:screenRect];
    
    // Set scrollview delegate
    scrollView.delegate = self;
    
    // Set the paging enabled to No
    scrollView.pagingEnabled = NO;
    
    // Add BNRHypnosisView as subview of window
    [self.window addSubview:scrollView];
    
    // Set scroll view's content size
    scrollView.contentSize = screenRect.size;
    
    // Scrollview maximum zoom
    scrollView.maximumZoomScale = 3;
    
    // Scrollview minimum zoom
    scrollView.minimumZoomScale = 0.5;
    
    // Create a super-sized hypnosis view and add it to the scroll view
    BNRHypnosisView *hv = [[BNRHypnosisView alloc]initWithFrame:screenRect];
    
    [scrollView addSubview:hv];
    
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

// Ask the delegate to scale when zooming
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    // Return value
    return self.hv;
}

#2

// Set scroll view's content size scrollView.contentSize = screenRect.size;

That’s what you’re doing wrong.
You’re telling the scrollView it’s content is exactly the same size as it’s own view bounds are. You use screenRect to set the frame for scrollView, so saying it’s content is the size of screenRect means it’ll think it’s as big, so there’s nothing there to zoom in on really.

Change it to to:

Good luck!


#3

Thanks for your feedback, but still I’m not able to zoom in and out the view.


#4

Alright, reading through your code something else I picked up on is that you do create a property for a hypnosisView, you use that property in the - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView method, but you never set it.
You create a new BNRHypnosisView named hv in your application didFinishLaunching method. Instead you should assign it to the property you created.

This:

[code]// Create a super-sized hypnosis view and add it to the scroll view
BNRHypnosisView *hv = [[BNRHypnosisView alloc]initWithFrame:screenRect];

[scrollView addSubview:hv];[/code]

Should be this:

self.hv = [[BNRHypnosisView alloc] initWithFrame:screenRect]; [scrollView addSubview:self.hv];


#5

Either way we are creating an instance of BNRHypnosisView, I don’t think that will solve this problem. I tried but couldn’t see any change.


#6

If you don’t set BNRHypnosisView to the property you won’t be able to use it outside of the method you call it in.
The way you had it before meant that you were doing nothing really in the other method in your BNRAppDelegate.

There really isn’t much else in your code that could be causing the problem.
I would suggest moving [self.window addSubview:scrollView]; under the setting of the zoomScale properties.
If that doesn’t fix it, compare it to my code and see where the difference is:

[code]#import “BNRAppDelegate.h”
#import “BNRHypnosisView.h”

@interface BNRAppDelegate ()

@property (nonatomic, strong) BNRHypnosisView *hypnosisView;

@end

@implementation BNRAppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    // Create CGRects for frames
    CGRect screenRect = self.window.bounds;
    CGRect bigRect = screenRect;
    bigRect.size.width *= 2.0;

    // Create a screen-sized scroll view and add it to the window
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:screenRect];
    scrollView.pagingEnabled = NO;
    scrollView.delegate = self;
    scrollView.maximumZoomScale = 2.5;
    scrollView.minimumZoomScale = 1.0;

    [self.window addSubview:scrollView];

    // Create a super-sized Hypnosis view and add it to the scroll view
    self.hypnosisView = [[BNRHypnosisView alloc] initWithFrame:screenRect];
    [scrollView addSubview:self.hypnosisView];

    // Tell the scroll view how big its content area is
    scrollView.contentSize = bigRect.size;

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
    }

  • (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
    {
    return self.hypnosisView;
    }

@end
[/code]


#7

Thanks for your help, I understood the mistake I was making. There is no problem with the code, but I was not completely aware of how to perform pinch to zoom operation in the simulator.
Just to assist others who gets confused with the code not giving proper execution, to perform pinch to zoom in the simulator " Press Option key while the pointer is on the view and user three fingers for zoom in and zoom out operation".


#8

I have the same code … but it’s not zooming for me. Also, clicking “Option” and dragging with 3 fingers does nothing. Don’t you zoom in and out by clicking “Option” and dragging a single finger around the mouse pad???

#import "BNRAppDelegate.h"
#import "BNRHypnosisView.h"

@interface BNRAppDelegate() <UIScrollViewDelegate>
    @property (nonatomic, strong) BNRHypnosisView * lview;
@end

@implementation BNRAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    // Create CGRects for frames
    CGRect screenRect = self.window.bounds;
    CGRect bigRect = screenRect;
    bigRect.size.width *= 2.0;

    // Create a screen sized scroll view and add it to the window
    UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:screenRect];
    // Setting the UIScrollView's delegate property
    scrollView.delegate = self;
    // Turn off paging
    scrollView.pagingEnabled = NO;
    // Add the scroll view as a sub view
    [self.window addSubview:scrollView];
    // Set scroll view's content size
    scrollView.contentSize = screenRect.size;
    // Scrollview maximum zoom
    scrollView.maximumZoomScale = 7;
    // Scrollview minimum zoom
    scrollView.minimumZoomScale = 0.5;
    
    // Create a screen-sized BNRHypnosisView and add it to the scroll view
    self.lview = [[BNRHypnosisView alloc]initWithFrame:screenRect];
    [scrollView addSubview:_lview];
 
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"TESTING");
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    NSLog(@"In viewForZoomingInScrollView...");
    return self.lview;
}

#9

I have a pretty similar solution, but why is everyone creating CGRect bigRect and not using it? Mine works without creating the oversize CGRect. Not sure if I am missing something.

[code]#import “JMRAppDelegate.h”
#import “JMRHypnosisView.h”
#import “JMRHypnosisViewController.h”
#import “JMRReminderViewController.h”
// edit, I didn’t need the UIKit, this was added when I was experimenting using some of the examples from UIScrollView in the help documentation
#import <UIKit/UIKit.h>

@interface JMRAppDelegate ()

@property (nonatomic, strong) JMRHypnosisView *view;

@end

@implementation JMRAppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];

    // create the normal sized window and the x and y scale values
    CGRect normalWindow = self.window.bounds;

    // create the scroll view, initialize to the normalWindow size
    UIScrollView *sv = [[UIScrollView alloc]initWithFrame:normalWindow];

    // set the delegate, paging enabled to No, and zoom scales
    sv.delegate = self;
    sv.pagingEnabled = NO;
    sv.minimumZoomScale = .25;
    sv.maximumZoomScale = 3.0;
    // add sv as subview to the window
    [self.window addSubview:sv];

    // initialize the hypnosis view and add it to the scroll view as a subview
    self.view = [[JMRHypnosisView alloc]initWithFrame:normalWindow];
    [sv addSubview:self.view];

    // set content size as normalWindow’s size
    sv.contentSize = normalWindow.size;

    // set the background color
    self.window.backgroundColor = [UIColor lightGrayColor];
    // show window and make it key
    [self.window makeKeyAndVisible];

    return YES;
    }

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
// return the hypnosis view
return self.view;
}[/code]


#10

[quote=“snaveen”]Thanks for your help, I understood the mistake I was making. There is no problem with the code, but I was not completely aware of how to perform pinch to zoom operation in the simulator.
Just to assist others who gets confused with the code not giving proper execution, to perform pinch to zoom in the simulator " Press Option key while the pointer is on the view and user three fingers for zoom in and zoom out operation".[/quote]

Had the exact same problem. :confused:
Thanks for sorting that out for me :smiley:


#11

it take me so much time in this excise.
and find out there is no problem in my code.
just because i don’t use there fingers in touch pad when using iOS simulator. :cry: