I add a view as sub view using the following code
imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageview setImage:cppobject->OutputImage];
imageview.contentMode = UIViewContentModeScaleAspectFit;
[holderView addSubview:imageview];
holderView.contentMode = UIViewContentModeScaleAspectFit ;
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
[rotationRecognizer release];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
[panRecognizer release];
-(void)scale:(id)sender {
}
-(void)rotate:(id)sender {
}
-(void)move:(id)sender {
}
-(void)tapped:(id)sender {
}
I need to draw line when the user use his two fingers to pan and a uilabel (the green one) like in the following image
I had look on How to draw graphics in iphone gesture?
But I couldn't apply it on the subview , specially the DrawInRect method
in panRecognizer function (move) I want to draw line using
UIPanGestureRecognizer *gR = (UIPanGestureRecognizer *) sender ;
NSValue *value = [NSValue valueWithCGPoint: [gR locationInView:gR.view]];
[Points addObject:value];
[holderView setNeedsDisplay];
NSLog(#"End of measuring") ;
and I will use the points in Points to draw line above all the subviews in
-(void)drawRect:(CGRect)rect {
NSLog(#"Entered Draw In Rect");
if (Measuring) {
[[UIColor redColor] setStroke];
UIBezierPath *pathToDraw = [UIBezierPath bezierPath];
for (int n = 1; n < [Points count] - 1 ; n++) {
NSValue * value = [Points objectAtIndex:(NSInteger)n];
CGPoint point = [value CGPointValue];
[pathToDraw moveToPoint:point];
value = [Points objectAtIndex:(NSInteger)n+1];
point = [value CGPointValue];
[pathToDraw addLineToPoint:point];
}
[pathToDraw stroke];
}
}
the problem is [holderView setNeedsDisplay]; never call or fire drawRect any suggestion or help regarding that
any suggestion
The image view being the subview draws on top of the holder view. The imageview is opaque. This means that when the views are drawn there is no part of the holder view that is actually visible, so it's drawRect call is optimised out.
Try ordering the views the other way around, so that the holder view is the subview of the imageview. Then the imageview will draw and the holder view will draw on top of it.
Also, note that you should use the bounds of the parent view as the frame of the subview.
UIView* subview = [[UIView alloc] initWithFrame:[parentview bounds]];
Edit (add):
See http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/WindowsandViews/WindowsandViews.html%23//apple_ref/doc/uid/TP40009503-CH2-SW1
, specifically under the section "View Hierarchies and Subview Management":
"Visually, the content of a subview obscures all or part of the content of its parent view"
So, try make the imageview the parent, do your initialisation like so:
// instance variables:
UIImageView* imageView;
MyHolderView* holderView;
imageView = [[UIImageView alloc] initWithFrame:mainRect];
holderView = [[MyHolderView alloc] initWithFrame:[imageView bounds]];
holderView.opaque = NO;
holderView.backgroundColor = [UIColor clearColor];
[imageView addSubview:holderView];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:holderView action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];
// etc...
Now, the image view is drawn, and the holder view, its subview is drawn on top of it. Now when you call setNeedsDisplay on the holderview it will receive a drawRect: call.
For example, track the gesture like so. This could be in your view controller or in your MyHolderView view subclass; the example here would be in the MyHolderView class so that the location1 and location2 instance variables can be shared easily with the drawRect: method.:
-(void)scale:(id)sender {
if (sender == pinchRecognizer) { // this allows the responder to work with multiple gestures if required
// get position of touches, for example:
NSUInteger num_touches = [pinchRecognizer numberOfTouches];
// save locations to some instance variables, like `CGPoint location1, location2;`
if (num_touches >= 1) {
location1 = [pinchRecognizer locationOfTouch:0 inView:holderView];
}
if (num_touches >= 2) {
location2 = [pinchRecognizer locationOfTouch:1 inView:holderView];
}
// tell the view to redraw.
[holderView setNeedsDisplay];
}
}
and then in the holder view drawRect routine:
-(void)drawRect:(CGRect)rect {
// use instance variables location1 and location2 to draw the line.
}
Related
By using CGPoint location it is always saving the last image in uiscrollview. When i m tapping on other image to save. What can i do to save the exact image one i tapped.
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
_image = [UIImage imageNamed:imageName];
_imageView = [[UIImageView alloc] initWithImage:_image];
_imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
imageScrollView.userInteractionEnabled = YES;
[imageScrollView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.delegate = self;
[gestureRecognizer release];
[imageScrollView addSubview:_imageView];
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
- (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Save Photo", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[actionSheet showInView:self.view];
[actionSheet release];
}}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0:
[self savePhoto];
break;
default:
break;
}
-(void)savePhoto{
CGPoint location = [gesture locationInView:_imageView];
if (CGRectContainsPoint(_imageView.bounds, location)){
UIImageWriteToSavedPhotosAlbum(_image, self, #selector(image: didFinishSavingWithError:contextInfo:), nil);
}}}
Any ideas will be appreciated.
Thanks
The point will always appear within the bounds of the UIScrollView in which the LongPressGestureRecognizer is triggered. You should check your scroll view's contentOffset (use contentOffset.x for horizontal layouts and contentOffset.y for vertical layouts) to detect which image you should save.
Additionally you could convert the touch point to the UIImageView instance's local coordinate system and see if the the point lies within the image view's bounds rect.
UPDATE
For example you could use something like this to detect if the point is within the image view's bounds (note: I have not tested this and this is assuming there is more than one image view added to the scroll view):
if (CGRectContainsPoint(_imageView.bounds, [self.view convertPoint:location toView:_imageView]))
{
// do something
}
You should also consider detecting which image should be saved before and storing a reference to that image before displaying the UIActionSheet to the user as it may decrease the number of potential issues you might encounter and will be easier to read later, but this is my subjective opinion.
I have implemented UITapGestureRecognizer on UIImageView, It is working on first tap. On First Tap, I am hiding that image and starting animation. Once the animations are completed, i am showing the image again. But After setting setHidden:FALSE, I am not getting the Tap event of that UIImageView.
Following is the code I am using :
- (void)viewDidLoad{
[super viewDidLoad];
defaultDogView= [[UIImageView alloc] initWithFrame:CGRectMake(3, 270, 110, 210)];
[defaultDogView setImage:[UIImage imageNamed:#"dog1.png"]];
defaultDogView.userInteractionEnabled = YES;
[self addGestureRecognizersToPiece:defaultDogView];
[self.view addSubview:defaultDogView];
}
- (void)addGestureRecognizersToPiece:(UIImageView *)piece
{
NSLog(#"in Gesture");
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapPiece:)];
[tapGesture setDelegate:self];
[piece addGestureRecognizer:tapGesture];
[tapGesture release];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressPiece:)];
[piece addGestureRecognizer:longPressGesture];
[longPressGesture release];
NSLog(#"%#", [piece gestureRecognizers]);
}
- (void)singleTapPiece:(UITapGestureRecognizer *)gestureRecognizer
{
NSLog(#"Image Tapped");
/** Hide the default Image and start the animation ***/
[defaultDogView setHidden:TRUE];
/***Animating the Dog***/
[dogArray addObject:[SpriteHelpers setupAnimatedDog:self.view numFrames:69 withFilePrefix:#"dog" withDuration:(12) ofType:#"png" withValue:0]];
dogView = [dogArray objectAtIndex:0];
//[self addGestureRecognizersToPiece:dogView];
[self performSelector:#selector(callBubbleUpdater) withObject:nil afterDelay:5.5];
}
-(void)showDogFrame{
NSLog(#"%#",[defaultDogView gestureRecognizers]);
[defaultDogView setHidden:FALSE];
defaultDogView.userInteractionEnabled = YES;
}
When view is hidden or its alpha component is zero that view won't receive any UIGestureRecognizers.
I can suggest to use next approach if you need to hide some view (let's name it touchableView) but want it to respond to gestures:
Create backgroundView with the same frame as touchableView:
UIView *backgroundView = [[UIView alloc] initWithFrame:touchableView.frame];
Set background color of backgroundView to clearColor:
backgroundView.backgroundColor = [UIColor clearColor];
Reset position of touchableView:
CGRect frame = touchableView.frame;
frame.origin.x = 0;
frame.origin.y = 0;
Disable user interaction of touchableView:
touchableView.userInteractionEnabled = NO;
Add touchableView as subview to backgroundView:
[backgroundView addSubview:touchableView];
Add appropriate gesture recognizers to backgroundView.
Add backgroundView to view that you want.
Now you can hide touchableView but you will still receive gesture recognizers.
I don't test this but I think it should work.
sure
when UIImageView is hidden. it does not receive any touch events
set alpha zero for uiimageview
For UITapGestureRecognizer, you can set number of taps required to control the recognition of the UITapGestureRecognizer. If you set numberOfTapsRequired to 2 and user taps only once, then the UITapGestureRecognizer won't be triggered.
My question is How about for UIPanGestureRecognizer? How to control its recognition?
I have a view. Once I set a UIPanGestureRecognizer to it, any dragging will trigger the action. But what I want is only the dragging in X-axis. And for non-X-axis dragging, all touch events should be sent to other views underneath.
How can I do it?
THanks
Set its delegate and implement
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
Then use
- (CGPoint)velocityInView:(UIView *)view;
on the gesture recogniser to calculate whether the gesture recognizer should handle it or not.
Check How to stop UIPanGestureRecognizer when object moved to certain frame. It may help you.
UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
UIImageView *imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageview setImage:image];
[holderView addSubview:imageview];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];
[self.view addSubview:holderView];
Raees
Assume that your gestureRecognizer triggers the action _panRecogPanned below. You can see how the center of a subview ( the view that carries the gesture recognizer itself) moves following the transition. To disable panning on y axis, you simply set the center as the calculated new center, whereas the translation.y is omitted.
To move other subviews on the y axis, get their frame, update their origin.x property and reset the frame, they should follow your finger only on the y axis.
- (IBAction)_panRecogPanned:(id)sender{
CGPoint translation = [_panRecog translationInView:_statementFilterView];
//This subview only moves horizontally
_panRecog.view.center = CGPointMake(translation.x + _panRecog.view.center.x, _panRecog.view.center.y);
//This subview only moves vertically
CGRect newFrame = anotherSubview.frame;
newFrame.origin.y = anotherSubview.frame.origin.y + translation.y;
anotherSubview.frame = newFrame;
[_panRecog setTranslation:CGPointMake(0, 0) inView:self.view];
}
Hey, guys, I met a problem when trying to removeGestureRecognizer: from a view,
what i want to do is doubleTap one of the imageViews, and remove the tapped imageView's singleTap Gesture, without remove other imageViews singleTap Gesture.
here is how i generate views, gestures and the mechanisms:
.h
UITapGestureRecognizer *singleTap;
.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSInteger i;
for (i = 1; i <= 3; i++)
{
UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(110, 70+80*(i-1), 100, 60);
imageView.backgroundColor = [UIColor whiteColor];
imageView.tag = i;
imageView.userInteractionEnabled = YES;
[self.view addSubview:imageView];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doubleMethod:)];
doubleTap.numberOfTapsRequired = 2;
singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleMethod:)];
[singleTap requireGestureRecognizerToFail:doubleTap];
[imageView addGestureRecognizer:doubleTap];
[imageView addGestureRecognizer:singleTap];
}
}
- (void)singleMethod: (id)sender
{
NSLog(#"SingleTap");
}
- (void)doubleMethod: (id)sender
{
NSLog(#"%d",[((UITapGestureRecognizer *)sender).view.gestureRecognizers count]);
UIImageView *imageView = nil;
NSArray *tryToFindYou = [self.view subviews];
for (imageView in tryToFindYou)
{
if ([imageView isKindOfClass:[UIImageView class]] && imageView.tag == ((UITapGestureRecognizer *)sender).view.tag)
{
[imageView removeGestureRecognizer:singleTap];
}
}
NSLog(#"%d",[((UITapGestureRecognizer *)sender).view.gestureRecognizers count]);
}
but these lines I wrote can't find exactly the singleTap Gesture attached to the double-tapped imageView.
when NSLog the .gestureRecognizers count, it still 2, what it removed is the last imageView's singleTap Gesture, it became 1, which is correct.
I can't locate the first and second one, any ideas to locate them? thank you for reading :)
You should cycle through gestureRecognizers property of the UIView class, where the gestures are added, something like this:
for (imageView in tryToFindYou)
{
if ([imageView isKindOfClass:[UIImageView class]] && imageView.tag == ((UITapGestureRecognizer *)sender).view.tag)
{
for(UIGestureRecognizer *gesture in [imageView gestureRecognizers])
{
if([gesture isKindOfClass:[UITapGestureRecognizer class]])
{
if (gesture.numberOfTapsRequired == 1)
[imageView removeGestureRecognizer:gesture];
}
}
}
}
I try to make following: Horizontal list of images, that I can select and do something with it. For example flip image around x axis. I know how to rotate image. I created scrollview and load it with images. I added event handler when I tap on image. But I don't know how to do something with tapped image. How to code method to do something with tapped image?
- (void)viewDidLoad {
[super viewDidLoad];
img1 = [UIImage imageNamed:#"imgTest.jpg"];
img2 = [UIImage imageNamed:#"imgTest2.jpg"];
arrayOfImages = [[NSMutableArray alloc] init];
[arrayOfImages addObject:img1];
[arrayOfImages addObject:img2];
[arrayOfImages addObject:img1];
[arrayOfImages addObject:img2];
scrollView = [[UIScrollView alloc] init];
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
scrollView.directionalLockEnabled = YES;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.delegate = self;
scrollView.backgroundColor = [UIColor blueColor];
scrollView.autoresizesSubviews = YES;
scrollView.frame = CGRectMake(0, 0, 320, 128);
[self.view addSubview:scrollView];
UIImage *imageToAdd;
int x = 0;
int y = 0;
for(imageToAdd in arrayOfImages)
{
UIImageView *temp = [[UIImageView alloc] initWithImage:imageToAdd];
temp.frame = CGRectMake(x, y, 128, 128);
temp.userInteractionEnabled = YES;
x += 135;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[temp addGestureRecognizer:tap];
[scrollView addSubview:temp];
}
...
- (void)imageTapped:(UIGestureRecognizer *)sender
{
// how to get reference on selected item in scrollview???
}
A gesture recognizer has a view property that returns the view associated with the recognizer. Since you know that it'll be a UIImageView you can simply cast it and use it as in:
UIImageView *iv = (UIImageView *)[sender view];
And your image can then be queried via:
UIImage *image = [iv image];
If you need to know the index in your array, there are two ways: either simply use [arrayOfImages indexOfObject:image];, or you can assign tags (numbers) to views and use them. A tag is not used by Apple, it's only here so we developers can "mark" views in some way. For example:
NSInteger counter = 0;
for(imageToAdd in arrayOfImages)
{
UIImageView *temp = [[UIImageView alloc] initWithImage:imageToAdd];
count.tag = counter++;
...
}
Then, in your imageTapped:, you can query the index via tag:
NSInteger index = [imageView tag];