Showing posts with label UIScrollView. Show all posts
Showing posts with label UIScrollView. Show all posts

Sunday, March 10, 2013

Delegation

UIScrollViewDelegate


The methods declared by the UIScrollViewDelegate protocol allow the adopting delegate to respond to messages from the UIScrollView class and thus respond to, and in some affect, operations such as scrolling, zooming, deceleration of scrolled content, and scrolling animations.

--------------------------


@implementation ViewController{
    UIImageView *myImageView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    
    // Create UIImageView to store image
    myImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"nature-wallpaper-hd1.jpg"]];
    
    // Set up scroll view content size to the size of the image
    self.myScrollView.contentSize = myImageView.frame.size;
    
    // Set yourself as delegate
    self.myScrollView.delegate = self;
    
    // Set the max zoom scale
    self.myScrollView.maximumZoomScale = 4.0f;
    
    // Add the image view onto scrollview
    [self.myScrollView addSubview:myImageView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIScrollViewDelegate methods
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    NSLog(@"The x offset of scroll is %f and y offset is %f", scrollView.contentOffset.x, scrollView.contentOffset.y);
}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return myImageView;
}

iOS Development - Navigation, Tab, Scroll

Intro to Navigation Controller
// Create instance of navigation controller
self.navController = [[UINavigationController alloc] init];
    
// Create instance of First View Controller
FirstViewController *firstVC = [[FirstViewController alloc]init];
    
// Push firstVC onto navigation stack
// animated set to NO so that you don't see the page sliding on first load
[self.navController pushViewController:firstVC animated:NO];
    
// Push entire stack onto window (canvas)
[self.window setRootViewController:self.navController];

----------------------------------------

Going to another page from a button in firstVC

- (IBAction)buttonPressed:(id)sender {
    
    // Push in 2nd view controller
    SecondViewController *secondVC = [[SecondViewController alloc]init];
    [self.navigationController pushViewController:secondVC animated:YES];
    
}

----------------------------------------

Intro to Tab Bar Controller

// Initialisation
    self.tabBarController = [[UITabBarController alloc] init];
    RedViewController *redVC = [[RedViewController alloc]init];
    BlueViewController *blueVC = [[BlueViewController alloc]init];

    NSArray *vcArray = [NSArray arrayWithObjects:redVC, blueVC, nil];
    [self.tabBarController setViewControllers:vcArray];
    [self.window setRootViewController:self.tabBarController];

----------------------------------------

Putting a Nav View Controller in a Tab Bar Controller

AppDelegate.m
    self.tabBarController = [[UITabBarController alloc] init];
    self.whiteNavController = [[UINavigationController alloc] init];
    
    RedViewController *redVC = [[RedViewController alloc]init];
    redVC.tabBarItem.title = @"Red";
    redVC.tabBarItem.image = [UIImage imageNamed:@"all.png"];
    
    BlueViewController *blueVC = [[BlueViewController alloc]init];
    blueVC.tabBarItem.title = @"Blue";
    blueVC.tabBarItem.image = [UIImage imageNamed:@"search.png"];
    
    WhiteViewController *whiteVC = [[WhiteViewController alloc]init];
    whiteVC.tabBarItem.title = @"White";
    whiteVC.tabBarItem.image = [UIImage imageNamed:@"faves.png"];
    [self.whiteNavController pushViewController:whiteVC animated:NO];
    
    NSArray *vcArray = [NSArray arrayWithObjects:redVC, blueVC, self.whiteNavController, nil];
    [self.tabBarController setViewControllers:vcArray];
    [self.window setRootViewController:self.tabBarController];

-------------------------------------

Scroll View
    // Create UIImageView to store image
    UIImageView *myImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"nature-wallpaper-hd1.jpg"]];
    
    // Set up scroll view content size to the size of the image
    self.myScrollView.contentSize = myImageView.frame.size;
    
    // Add the image view onto scrollview
    [self.myScrollView addSubview:myImageView];