user grab and move an image in app iPhone - iphone

I am trying to have a user grab and move an image in an app I am building.
I have created a UIImageView like this
DragView.h
#import <UIKit/UIKit.h>
#interface DragView : UIImageView{
}
#end
and
DragView.m
#import "DragView.h"
#implementation DragView
-(void) touchesMoved:(NSSet *)set withEvent:(UIEvent *)event {
CGPoint p = [[set anyObject] locationInView:self.superview];
self.center = p;
}
#end
I then imported the .h file into my main View controller and assigned DragView to a UIImageView in the IB Identity Inspector.
I must be missing something because when I build and run and then touch the image it does't move. What and I missing?

Related

No visible #interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'

I've got this code from this post and everything is ok except one thing, I have this error that I dont know how to fix. this is the code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint touchPoint = [[[event touchesForView:self] anyObject] locationInView:self];
CGRect currentBounds = self.bounds;
CGFloat x = touchPoint.x - CGRectGetMidX(currentBounds);
if(x<0 && self.currentPage>=0){
self.currentPage--;
[self.delegate pageControlPageDidChange:self];
}
else if(x>0 && self.currentPage<self.numberOfPages-1){
self.currentPage++;
[self.delegate pageControlPageDidChange:self];
}
}
the error is that:
No visible #interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'.
I've tried to run this code with ARC, I removed the dealloc method, changed assign to weak in here:
#property (nonatomic, weak) NSObject<PageControlDelegate> *delegate;
as someone wrote in the answers, but still it isn't working.
Please help me.
If in your header you just have:
#protocol PageControlDelegate;
You missed the actual definition of what the protocol is. At the bottom of that first code block in your linked post there is this code that contains the declaration of the missing protocol function:
#protocol PageControlDelegate<NSObject>
#optional
- (void)pageControlPageDidChange:(PageControl *)pageControl;
#end
You need to import the .h file that declares the PageControlDelegate protocol.

How do I apply a UILongPressGestureRecognizer to a shape in C4?

I am working in C4 and want to be able to press down on a shape and drag it at a slow speed upward.
I have been working with the "Getting the UITouch objects for a UIGestureRecognizer" tutorial and other TouchesMoved and TouchesBegan Objective-C tutorials but the gesture is not applying to the shape. The project builds but the shape still does not move when you press and drag it. Is there a particular way C4 applies gestures to shapes?
This is my code:
DragGestureRecognizer.h
#import <UIKit/UIKit.h>
#interface DragGestureRecognizer : UILongPressGestureRecognizer {
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
#end
#protocol DragGestureRecognizerDelegate <UIGestureRecognizerDelegate>
- (void) gestureRecognizer:(UIGestureRecognizer *)gr movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event;
#end
C4WorkSpace.m:
#import "C4WorkSpace.h"
#import "DragGestureRecognizer.h"
#implementation DragGestureRecognizer
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
if ([self.delegate respondsToSelector:#selector(gestureRecognizer:movedWithTouches:andEvent:)]) {
[(id)self.delegate gestureRecognizer:self movedWithTouches:touches andEvent:event];
}
}
#end
#implementation C4WorkSpace {
C4Shape *circle;
}
- (void)longPress:(UILongPressGestureRecognizer*)gesture {
if ( gesture.state == UIGestureRecognizerStateEnded ) {
NSLog(#"Long Press");
}
}
-(void)setup {
circle = [C4Shape ellipse:CGRectMake(5,412,75,75)];
[self.canvas addShape:circle];
}
#end
C4 has a method that simplifies the process of adding gestures to any visible object (e.g. C4Shape, C4Movie, C4Label...).
NOTE: you will need to create a subclass of C4Shape in which you can create some custom methods.
For instance, the following will add a tap gesture to an C4Shape:
C4Shape *s = [C4Shape rect:CGRectMake(..)];
[s addGesture:TAP name:#"singleTapGesture" action:#"tap"];
The addGesture:name:action: method is listed in the C4Control documentation, and is defined like:
Adds a gesture to an object.
- (void)addGesture:(C4GestureType)type name:(NSString *)gestureName action:(NSString *)methodName
The (C4GestureType)type can be any one of:
TAP
SWIPERIGHT
SWIPELEFT
SWIPEUP
SWIPEDOWN
PAN
LONGPRESS
The following gestures are available, but haven't yet been tested:
PINCH
ROTATION
The (NSString *)gestureName allows you to specify a unique name for the gesture.
The (NSString *)methodName allows you to specify the method that the gesture will trigger.
So... getting back to the example above:
[s addGesture:TAP name:#"singleTapGesture" action:#"tap"];
...will add to the s object a TAP gesture called singleTapGesture which, when triggered, will run a -(void)tap; method (which needs to have already defined in your class).
You can apply the technique I described above, but instead use:
[s addGesture:PAN name:#"panGesture" action:#"move:];
you will also have to define a
-(void)move:(id)sender;
method in your shape's class.

iphone paint on top / overlay with event pass through

I would like to be able to paint on top of my subviews, or in other words: have an overlay that does not block the events. So far I discovered:
- any instructions in drawRect are painted below subviews,
- putting a transparent UIView on top blocks events.
Is there another trick I can try?
Use a transparent UIView on top, and in IB uncheck "User Interaction Enabled" for that view, then input events will go down to your controls beneath it.
Or, in code do:
UIView *overlayView = [[UIView alloc] init...];
overlayView.userInteractionEnabled = NO;
To solve this you want to forward the hitTest events. Add the code below to your project, add a UIImageView to your interface, set its Class to ClickThroughImageView and connect the "onTopOf" outlet to whatever UIView is below the image.
The ClickThroughImageView.h file:
#import <Foundation/Foundation.h>
#interface ClickThroughImageView : UIImageView
{
IBOutlet UIView *onTopOf;
}
#property (nonatomic, retain) UIView *onTopOf;
#end
The ClickThroughImageView.m file
#import "ClickThroughImageView.h"
#implementation ClickThroughImageView : UIImageView
#synthesize onTopOf;
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
return [onTopOf hitTest:point withEvent:event];
}
#end

Using UITouch To Drag A UITextField Around The Screen on iPhone

I am developing a sample application, where I have a situation for moving a UITextFields, UILabels etc., across the screen. I am not able to find any resource for implementing this.
I want to implement like this for a UITextField
Since you have an excellent tutorial on dragging already, I will assume your problem is the keyboard that comes up when you try to drag a UITextField instead of a UIView. The solution should be pretty simple: myTextField.userInteractionEnabled = NO; - that should disable user interaction and make it "read only". Perhaps have an edit mode where all text fields get this flag set. If it causes trouble, then add the textField to a UIView and then set the userInteractionEnabled to false. Then you can drag the UIView and it will drag the text field with it.
I followed Michael's suggestion and got the solution.I have given the code snippets below which will be useful for those who need to implement the same.
Steps:
Choose windows based application,then create a UIViewController subclass and add it to the window in the appdelegate file.
In the XIB of the viewcontroller class you created add UIViews and add the controls like textfield etc.,to the UIViews that you have created.We are going to move these views only,so add IBOutlets in the .h file of the view controller subclass and map them to the IB accordingly.
Sample Code
appdelegate.h
#import <UIKit/UIKit.h>
#import "MyView.h"
#interface MyAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MyView *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
appdelegate.m
#import "MyAppDelegate.h"
#implementation MyAppDelegate
#synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch.
viewController=[[MyView alloc]init];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#end
viewcontroller.h
#import <UIKit/UIKit.h>
#interface MyView : UIViewController {
IBOutlet UIView *textFieldView;
IBOutlet UIView *labelView;
}
#end
viewcontroller.m
#import "MyView.h"
#implementation MyView
- (void)viewDidLoad {
[self.view addSubview:textFieldView];
[self.view addSubview:labelView];
[super viewDidLoad];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// get touch event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if ([touch view] == textFieldView) {
// move the image view
textFieldView.center = touchLocation;
}
if ([touch view] == labelView) {
// move the image view
labelView.center = touchLocation;
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
Thank u all.Have a niece time.

Double Tap inside UIScrollView to Another View

I want to preface this question with the fact I am new to the iphone application development and many times believe I may be over my head. I am trying to find an action that will allow me to double tap anywhere within my full screen UIScrollView to return to my main menu (MainMenuViewController).
I currently have the following code running for my ScrollViewController...
ScrollViewController.h
#import <UIKit/UIKit.h>
#interface ScrollViewController : UIViewController <UIScrollViewDelegate>
{
}
#end
ScrollViewController.m
#import "ScrollViewController.h"
UIScrollView *myScrollView;
UIPageControl *myPageControl;
#implementation ScrollViewController
- (void)loadScrollViewWithPage:(UIView *)page
{
int pageCount = [[myScrollView subviews] count];
CGRect bounds = myScrollView.bounds;
bounds.origin.x = bounds.size.width * pageCount;
bounds.origin.y = 0;
page.frame = bounds;
[myScrollView addSubview:page];
}
...etc
Any advice or sample code to implement a double tap within the ScrollView Controller to allow me to return to my MainMenuViewController would be greatly appreciated. Also please include any Interface Builder changes to the view (if necessary). I have been pouring over the forums for days and have not been able to successfully implement this action. Thanks...R.J.
First of all, create a subclass of a UIScrollView:
#import <UIKit/UIKit.h>
#interface MyScrollView : UIScrollView {
}
#end
Implement it:
#import "MyScrollView.h"
#implementation MyScrollView
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event {
if (!self.dragging) {
[self.nextResponder touchesEnded: touches withEvent:event];
}
[super touchesEnded: touches withEvent: event];
}
- (void)dealloc {
[super dealloc];
}
#end
Then, use this subclass instead of a normal UIScrollView in your main View Controller. This will call the touchesEnded:withEvent: method (alternatively, you can call any method you want). Then, track for double taps (if you need info on how to do that, let me know).