touch events in UIScrollview - iphone

I'm new to iPhone development. I have so many UIImageViews added as subview at runtime to the self.view.
My current architecture is:
Inside UIView there is a UIScrollView and inside UIScrollView there is another UIView which is attached to a UIView sub class named SeatingPlan.
SeatingPlan only draws the grid on the screen.
The UIImageViews are drawn on the screen the by UIViewController sub class named SeatingPlanViewContoller. The UIImageViews are added as the subviews.
The UIImageViews are saved in an NSMutableArray.
The .h file:
#interface SeatingPlanViewController : UIViewController <UIScrollViewDelegate, UIAlertViewDelegate>{
IBOutlet UIScrollView * scrollView;
SeatingPlan * planView;
CGPoint origin;
NSMutableArray * arrayOfImages;
}
The .m file:
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationItem setTitle:#"Seating Planner"];
UIBarButtonItem *mySave = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(Save)];
self.navigationItem.rightBarButtonItem = mySave;
[mySave release];
segmentController.selectedSegmentIndex = UISegmentedControlNoSegment;
segmentController.momentary = YES;
scrollView.contentSize=CGSizeMake(1280.0,960.0);
[scrollView setBackgroundColor:[UIColor whiteColor]];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
[planView addSubview:scrollView];
[self.view addSubview:planView];
}
I want to move the UIImageViews on the screen. I would prefer some sample code.
Where should I put my touch handling code? In the view controller subclass or in the UIView subclass?

Related

Prevent custom UINavigationBar elements from changing between views

Simple question: in the main view controller of my app (which is in a navigation controller), I am customizing the nav bar with something like this:
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"titleImage"]];
UIButton *menuButton = [[UIButton alloc] init];
[menuButton setImage:[UIImage imageNamed:#"menuIcon"] forState:UIControlStateNormal];
[menuButton setFrame:CGRectMake(0, 0, 34, 34)];
UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithCustomView:menuButton];
self.navigationItem.rightBarButtonItem = menuItem;
I want these elements - the title view and the right bar button - to remain consistent throughout the app as I push and pop new view controllers onto and off of my navigation controller.
Of course, I could just set my custom items up in viewDidLoad of every view controller that is pushed onto my navigation stack, but this means that during the animation between two view controllers, my custom items are animated in and out, which is not as clean as I would like.
Any suggestions on how I would go about maintaining those custom elements on my nav bar when switching from vc to vc? Thanks!
You could create your own navigation item class that subclasses UIButton and set all the appearance in that class, then just set your navigation items as those custom UIButtons.
Something like this:
In CustomButton.h
#import <UIKit/UIKit.h>
#interface CustomButton : UIButton
#end
Then in CustomButton.m
#implementation CustomButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//Set images etc.
[menuButton setImage:[UIImage imageNamed:#"menuIcon"] forState:UIControlStateNormal];
[menuButton setFrame:CGRectMake(0, 0, 34, 34)];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
Then in your viewController:
#import CustomButton.h
- (void)viewDidLoad
{
CustomButton *button = [[CustomButton alloc]init];
self.navigationItem.leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:button];
}

Trying to base some code off of Apple's Image Zooming App example, missing some simple Interface Builder knowledge

I'm trying to base part of my app off of Apple's Image Zooming example, to get zooming, scrolling, and orientation of images that are saved to the app's sandbox.
http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/UIScrollView_pg/Introduction/Introduction.html
I have it sort of working now, except that when the ScrollView loads, it's blank. The code looks something like this:
#interface PhotoViewController : UIViewController <UIScrollViewDelegate> {
UIScrollView *imageScrollView;
UIImageView *imageView;
}
#property (nonatomic, retain) IBOutlet UIScrollView *imageScrollView;
#property (nonatomic, retain) UIImageView *imageView;
#end
#implementation PhotoViewController
#synthesize imageView, imageScrollView;
- (void)viewDidLoad {
[super viewDidLoad];
imageScrollView.bouncesZoom = YES;
imageScrollView.delegate = self;
imageScrollView.clipsToBounds = YES;
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image1.jpg"]];
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.backgroundColor = [UIColor blackColor];
imageView.userInteractionEnabled = YES;
[imageScrollView addSubview:imageView];
imageScrollView.contentSize = [imageView frame].size;
}
The xib has a Photo View Controller->Scroll View->View structure. I have a feeling this is where the problem is. I tried to hook up all the outlets identically to the example, but under Referencing Outlets the example has a viewController hooked up to the ImageZoomingAppDelegate. Since my PhotoViewController is a subview, there's not a place to hook up the viewController like that.
Here's how I bring up the PhotoViewController:
(IBAction) photoButtonPressed: (id) sender {
viewController = [[PhotoViewController alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
}
I know I must be just "this close" to having it all hooked up right, but I don't understand the relationship of the xib to the code well enough to know how to debug it. I'm not even sure if I know enough to ask the right questions.
Try changing viewController = [[PhotoViewController alloc] init]; to
viewController = [[PhotoViewController alloc] initWithNibName:#"PhotoViewController" bundle:nil];
(of course change the text string to whatever you called your xib file).

How to add a Subview using [self.view addSubview: myView] where myView is subclass of UIView?

In my Project, I have a customised #interface GraphView: UIView. Hence GraphView is a subclass of UIView and is meant to show a graph.
Then, I create a new View Controller called Summary using a NIB. In the Interface builder, I just add a UIToolbar at the bottom of the View.
In the implementation of Summary, in the viewDidLoad method, I have the following code:
-(void)viewDidLoad {
[super viewDidLoad];
GraphView *myGraph = [[GraphView alloc]init];
[self.view addSubview:myGraph]; //this does not work
}
But I am unable to get this.
I believe you need to set the frame of myGraph before you add it as a subview to self.
CGRect rect = CGRectInset(self.view.bounds, 0.0, 0.0);
GraphView *myGraph = [[GraphView alloc] initWithFrame:rect];
[self.view addSubview:myGraph];

Hiding master view in split view app..?

I have created split view based ipad app, where master view is table view while Detail view display images.. I need to display the image fit to screen 100% in landscape mode.
This could be on button event or double tap event.. How should i do that.
Thanks in advance.
You can accomplish what you want by using a secondary window in your app that you display on-demand on top of your main window that contains the split view.
Create a new UIWindow & a new UIViewController. Add the UIViewController's view to your new window, set the window level to a positive value (1 or more) so that it is on top of your main window, then put the new window onscreen. If you set the window background color to [UIColor clearColor] and position your image in a view inside the new UIViewController directly on top of the image that is in the detail view then the user won't notice that anything new has happened. You can then animate the image frame up to fullscreen or do whatever you want. We sometimes use this technique to support drag & drop or our own custom modal view controllers but it'll work for your purpose too.
Here's an example:
#interface MyViewController : UIViewController #end
#interface AppDelegate : NSObject <UIApplicationDelegate> {
MyViewController *overlayController;
UIWindow *overlayWindow;
UIWindow *window; // the main window that contains your splitview
UINavigationController *navigationController; // or split view contoller, whatever, your main controller
}
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
#implementation MyViewController
- (void) loadView {
self.view = [[[UIView alloc] initWithFrame: CGRectZero] autorelease];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.view.backgroundColor = [UIColor redColor];
}
#end
#implementation AppDelegate
#synthesize window, navigationController;
- (void) click:(id) sender {
[overlayController.view removeFromSuperview];
[overlayController release];
overlayController = nil;
overlayWindow.hidden = YES;
[overlayWindow release];
overlayWindow = nil;
}
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Add the navigation controller's view to the window and display.
// standard stuff...
[self.window addSubview: navigationController.view];
[self.window makeKeyAndVisible];
// add the overlay window
// note that both the overlay window and controller are retained until we dismiss
// the window, this is important!
overlayWindow = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].applicationFrame]; // or [UIScreen mainScreen].bounds, depending on what you want
overlayController = [MyViewController new];
overlayController.view.frame = overlayWindow.bounds;
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[button addTarget: self action: #selector(click:) forControlEvents: UIControlEventTouchUpInside];
[button setTitle: #"Done" forState: UIControlStateNormal];
button.frame = CGRectMake( 0, 0, 100, 50 );
button.center = overlayController.view.center;
[overlayController.view addSubview: button];
// the controller's view is the first and only view in the
// new window. this ensures you get rotation events. Add any subviews
// that will appear in the new window to overlayContoller.view
[overlayWindow addSubview: overlayController.view];
[overlayWindow setWindowLevel: 1];
[overlayWindow makeKeyAndVisible];
return YES;
}
- (void)dealloc {
[overlayController release];
[overlayWindow release];
[navigationController release];
[window release];
[super dealloc];
}
#end

UITouches not detecting

My UItouches is not detecting in my Slideshow nib file. What is the problem? Can anyone help?
#class Slideshow;
#interface RootViewController : UIViewController{
PreferencesController *preferencesController;
Slideshow *slideshow;}
Slideshow Implementation
#implementation Slideshow
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
NSLog(#"touches begin");
}
- (void)viewDidLoad
{
UIImageView *frontView = [[UIImageView alloc] initWithFrame:self.view.bounds];
frontView.backgroundColor = [UIColor blackColor];
frontView.image = [UIImage imageNamed:#"apple.png"];
frontView.userInteractionEnabled = YES;
[self.view addSubview:frontView];
[frontView release];
}#end
Slideshow is a UIViewController and touchesBegan is only called on a UIView. You need to subclass UIView to capture touches.