I'm an Objective-C beginner and I've found similar answers on stackoverflow to my question, but even after trying all of the tips/advice I'm still lost.
I have an IBOutlet UIImageView, and I want it to be so that if the user swipes up or down on this Image, then the position of various images on the view controller change.
I'm very green with the UISwipegesturerecognizer coding, so please explain in a way a beginner would understand. Thank you very much in advance!!!
UPDATE:
#Axeva, I used the following code, and there are no caution warnings or errors. However, it isn't executing properly on my simulator. Here is my code :
interface math0 (){
UISwipeGestureRecognizer *swipeUp;}
- (void)viewDidLoad
{
[super viewDidLoad];
swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: #selector(swipedScreenUp:)];
[swipeUp setNumberOfTouchesRequired: 1];
[swipeUp setDirection: UISwipeGestureRecognizerDirectionUp];
[one addGestureRecognizer: swipeUp];
[swipeUp setEnabled: NO];
}
- (void)swipedScreenUp:(UISwipeGestureRecognizer*)swipeGesture {
// Sampling to see if it works
instructions.text = [NSString stringWithFormat:#"IT WORKED!"];
}
In my .h file, I declared - (void)swipedScreenUp:(UISwipeGestureRecognizer)swipeUp;. On my storyboard, I set my image (named one) so that it was accessibility and user interaction enabled.
I've also tried with [swipeUp setEnabled: YES]; in my viewDidLoad file.
Can you or anyone tell me where the error is? Thank you!!
Try something like this:
#interface YourViewController () {
UISwipeGestureRecognizer *swipeLeftToRightGesture;
}
- (void)viewDidLoad {
[super viewDidLoad];
swipeLeftToRightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: #selector(swipedScreenRight:)];
[swipeLeftToRightGesture setNumberOfTouchesRequired: 1];
[swipeLeftToRightGesture setDirection: UISwipeGestureRecognizerDirectionRight];
[[self view] addGestureRecognizer: swipeLeftToRightGesture];
}
- (void)swipedScreenRight:(UISwipeGestureRecognizer*)swipeGesture {
// Move your image views as desired
}
You can adjust this basic code to do exactly what you wish. (different swipe directions; etc.)
Related
It might be helpful to know that I come from a iOS background and that this is one of my first OS X projects.
In my project, I have a NSView subclass and a NSViewController and load the view in the controller.
- (void)loadView
{
StartView *view = [StartView alloc] initWithFrame:frame]; // frame exists
self.view = view;
}
The view has a NSButton-property btnNewthat I add in initWithFrame:.
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.btnNew = [[NSButton alloc] initWithFrame:NSRectFromCGRect(CGRectMake(350, 180, 300, 60))];
[self addSubview:self.btnNew];
}
return self;
}
In loadView: I try to set the action and target.
- (void)loadView
{
StartView *view = [StartView alloc] initWithFrame:frame]; // frame exists
[view.btnNew setTarget:self];
[view.btnNew setAction:#selector(createNew:)];
self.view = view;
}
The createNew: function exists, off course.
However, when clicking the button, the app crashes. When I move the setTarget: and the setAction: into the view and perform them on self.btnNew, it works, but I think that's not how you should work with events and user interaction in a decent MVC-architecture.
The problem is with the self in [view.btnNew setTarget:self];, I guess. If I grab the target with view.btnNew.target, this is a nil object (0x000…000). Self exists & view.btnNew exists.
Am I doing something wrong? Or should I really listen to the click in the view and work with a delegate or a notification (I guess not?)
I found it out myself, with help from Niel Deckx.
The problem was ARC. The ViewController got dealloced before the click happened, which explains the NSObject does not respond to selector: the target doesn't exist anymore.
The easy solution is to have the ViewController as a (strong?) property where you create it.
Using the GestureRecognizer attached to a view triggers my app to crash with EXC_BAD_ACCESS error. Here's the classes involved
• BoardViewController - Displaying a board (as background) set as rootViewController in the AppDelegate. It instantiates multiple objects of the "TaskViewcontroller".
//BoardViewController.h
#interface BoardViewController : UIViewController {
NSMutableArray* allTaskViews; //for storing taskViews to avoid having them autoreleased
}
//BoardViewController.m - Rootviewcontroller, instantiating TaskViews
- (void)viewDidLoad
{
[super viewDidLoad];
TaskViewController* taskA = [[TaskViewController alloc]init];
[allTaskViews addObject:taskA];
[[self view]addSubview:[taskA view]];
}
• TaskViewController - An indivual box displayed on the board. It should be draggable. Therefore I attached UIPanGestureRecoginzer to its view
- (void)viewDidLoad
{
[super viewDidLoad];
UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan:)];
[[self view] addGestureRecognizer:panRecognizer];
}
- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
NSLog(#"PAN!");
}
The .xib file is a simple view.
All programming with the gesture recognizer I'd prefer to do in code. Any idea how to fix the error causing the app crash?
The method handlePan is on your view controller, not on your view. You should set the target to self:
UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan:)];
EDIT (in response to the edit of the question) As omz has correctly noted, your TaskViewController gets released upon BoardViewController's viewDidLoad: exit. There are two ways of dealing with it:
Fold the handlePan method into the parent view controller, along with the code of viewDidLoad:, or
Make an instance variable for TaskViewController *taskA, rather than making it a local variable.
This is my way to use Gesture Recognizer. I think this way is easy and with low risk.
At first, you drag and drop Gesture Recognizer into the view.
Then, you wire the Gesture Recognizer icon to code.
Finally, you write code for this IBAction like below:
- (IBAction)handlePan:(id)sender {
NSLog(#"PAN!");
}
You can download this project from GitHub and just run it.
https://github.com/weed/p120812_PanGesture
I've got a QuickLook view that I view some of my app's documents in. It works fine, but I'm having my share of trouble closing the view again. How do I create a touch event / gesture recognizer for which I can detect when the user wants to close the view?
I tried the following, but no events seem to trigger when I test it.
/------------------------ [ TouchPreviewController.h ]---------------------------
#import <Quicklook/Quicklook.h>
#interface TouchPreviewController : QLPreviewController
#end
//------------------------ [ TouchPreviewController.m ]---------------------------
#import "TouchPreviewController.h"
#implementation TouchPreviewController
- (id)init:(CGRect)aRect {
if (self = [super init]) {
// We set it here directly for convenience
// As by default for a UIImageView it is set to NO
UITapGestureRecognizer *singleFingerDTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSingleDoubleTap:)];
singleFingerDTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:singleFingerDTap];
[self.view setUserInteractionEnabled:YES];
[self.view setMultipleTouchEnabled:YES];
//[singleFingerDTap release];
}
return self;
}
- (IBAction)handleSingleDoubleTap:(UIGestureRecognizer *) sender {
CGPoint tapPoint = [sender locationInView:sender.view.superview];
[UIView beginAnimations:nil context:NULL];
sender.view.center = tapPoint;
[UIView commitAnimations];
NSLog(#"TouchPreviewController tap!" ) ;
}
// I also tried adding this
- (BOOL)gestureRecognizer:(UIGestureRecognizer *) gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*) otherGestureRecognizer {
return YES;
}
#end
Edit: For clarification, this is how I instantiate the controller:
documents = [[NSArray alloc] initWithObjects: filename , nil ] ;
preview = [[TouchPreviewController alloc] init];
preview.dataSource = self;
preview.delegate = self;
//set the frame from the parent view
CGFloat w= backgroundViewHolder.frame.size.width;
CGFloat h= backgroundViewHolder.frame.size.height;
preview.view.frame = CGRectMake(0, 0,w, h);
//refresh the preview controller
[preview reloadData];
[[preview view] setNeedsLayout];
[[preview view] setNeedsDisplay];
[preview refreshCurrentPreviewItem];
//add it
[quickLookView addSubview:preview.view];
Also, I've defined the callback methods as this:
- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{
return [documents count];
}
- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index
{
return [NSURL fileURLWithPath:[documents objectAtIndex:index]];
}
Edit2: One thing i noticed. If I try making swiping gestures, I get the following message. This could shed some light on what is wrong/missing?
Ignoring call to [UIPanGestureRecognizer setTranslation:inView:] since
gesture recognizer is not active.
I think your example code is incomplete. It isn't clear how you are instantiating the TouchPreviewController (storyboard, nib file or loadView.)
I have never used the class so I could be way out in left field.
If you've already instantiated a UITapGestureRecognizer in the parent viewController, it is absorbing the tap events and they aren't passed on to your TouchPreviewController.
I would implement the view hierarchy differently by attaching the UITapGestureRecognizer to the parent viewController and handle presentation and unloading of the QLPreviewController there.
I think you might not have to subclass QLPreviewController by instantiating the viewController from a nib file.
When your parent viewController's UITapGestureRecognizer got an event you would either push the QLPreviewController on the navigation stack or pop it off the navigation stack when done.
Hope this is of some help.
I have a very custom table view that actually serves as a content view, but table view was the obvious choice. I have a section index that i use to scroll the TableView - but there are no sections (well, one is there obviously). For the purpose of the user's orientation, I'd like to fade a view over the table view that is semi-transparent and shows a text in there. It should look like the overlay with the letters when scrolling the new iPod nano's section index. I don't know where i should put the code - because my view has to disappear sometime again too, and I don't really wanna use notifications. I'd init the view inside the tableview: sectionForSectionIndexTitle method. Thanks in advance.
Create a property in your .h file
#property (nonatomic, retain) UILabel *overlayLabel;
And add the following code to your .m file
- (void)viewDidLoad {
[super viewDidLoad];
self.overlayLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0f,
0.0f,
self.tableView.frame.size.width,
self.tableView.frame.size.height)] autorelease];
overlayLabel.backgroundColor = [UIColor clearColor];
overlayLabel.alpha = .5f;
overlayLabel.textAlignment = UITextAlignmentCenter;
overlayLabel.text = #"Some text";
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView addSubview:overlayLabel];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[overlayLabel removeFromSuperview];
}
I am writing an appcelerator module, which means I am handed a subclassed UIView to work with and create my visual controls in Objective C.
I am trying to add a tableview with a searchbar, but most samples online use rootViewController and UITableViewControler.
so...in order to add a tableview to the current view, do I need to create a tableview and a UITableViewController and add them somehow as subviews to the current view ?
I tried adding a MainViewController.h & MainViewController.m which is defined as
#interface MainViewController : UITableViewController <UISearchDisplayDelegate, UISearchBarDelegate>
and then in my view
#import "MainViewController.h"
- (void)viewDidLoad
{
mainView = [[MainViewController alloc] init];
[self addSubview:mainView.view];
}
-(void)frameSizeChanged:(CGRect)frame bounds:(CGRect)bounds
{
if(CGRectIsEmpty(self.frame))
{
self.frame = bounds;
[self addSubview:mainView.view];
}
}
but it did not work, I just got an empty view. any ideas ? a sample code would be greatly appreciated
thanks
You could try something like this:
- (void)viewDidLoad {
self.view.backgroundColor=[UIColor whiteColor];
UITableView *TableListView=[[UITableView alloc] initWithFrame:CGRectMake(-5,-1,331,425) style:1];
TableListView.editing=NO;
TableListView.delegate=self;
TableListView.dataSource=self;
TableListView.separatorColor=[UIColor colorWithRed:0.000000 green:0.591928 blue:1.000000 alpha:1.000000];
TableListView.separatorStyle=1;
TableListView.rowHeight=40;
TableListView.tag=0;
TableListView.backgroundColor=[UIColor groupTableViewBackgroundColor];
TableListView.clipsToBounds=YES;
[self.view addSubview:TableListView];
[TableListView release];
}
Hope this helps get you started...