Swipe Subview Off Screen - ios

I have an image view as a subview and I'm trying to implement a swipe up gesture to fling it off screen. The swipe works but it doesn't animate.
This is how I'm adding the subview:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
InstagramMedia *media = mediaArray[indexPath.row];
for (int i = 0; i < mediaArray.count; i++)
{
InstagramMedia *instagramMedia = [mediaArray objectAtIndex:i];
[imageArray addObject:instagramMedia.standardResolutionImageURL];
}
largeImageArray = imageArray;
imageIndex = indexPath.row;
UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:indexPath];
CGRect cellRect = attributes.frame;
blurredView = [[UIToolbar alloc]initWithFrame:self.view.bounds];
[blurredView setBarStyle:UIBarStyleBlackOpaque];
[self.view addSubview:blurredView];
imageView = [[UIImageView alloc]initWithFrame:cellRect];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.userInteractionEnabled = YES;
[imageView setImageWithURL:media.standardResolutionImageURL placeholderImage:[UIImage imageNamed:#"placeholder"]];
[self.view addSubview:imageView];
CGRect finalFrame = CGRectMake(0, 50, 320, 301);
[UIView animateWithDuration:0.5 animations:^{
imageView.frame = finalFrame;
}];
UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeLeft:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeRecognizer];
UISwipeGestureRecognizer *swipeRightRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeRight:)];
swipeRightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRightRecognizer];
swipeUpRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeUp:)];
swipeUpRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeUpRecognizer];
}
And this is what I'm trying to do:
- (IBAction)swipeUp:(id)sender
{
CGRect finalFrame = CGRectMake(0, -100, 320, 301);
[UIView animateWithDuration:0.5 animations:^{
imageView.frame = finalFrame;
[imageView removeFromSuperview];
[blurredView removeFromSuperview];
[self.view removeGestureRecognizer:swipeUpRecognizer];
}];
}

- (IBAction)swipeUp:(id)sender
{
CGRect finalFrame = CGRectMake(0, -100, 320, 301);
[UIView animateWithDuration:0.5 animations:^{
imageView.frame = finalFrame;
} completion:^(BOOL finished) {
[imageView removeFromSuperview];
[blurredView removeFromSuperview];
[self.view removeGestureRecognizer:swipeUpRecognizer];
}];
}
You were removing view before the animation ends... that's why its' happening.

Related

iOS - UILabels and Gestures

I have UILabels that get animated which move left and right on the screen, and I want to be able to drag them even while they are being animated. Anyone have any idea how to do that?
WordLabel *firstWordLabel = [[WordLabel alloc] initWithFrame:CGRectMake(x, 100, 10, 35) andWithWord:firstWord];
firstWordLabel.text = firstWord.word;
[firstWordLabel sizeToFit];
firstWordLabel.userInteractionEnabled = YES;
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:#selector(labelDragged:)];
[firstWordLabel addGestureRecognizer:gesture];
[self.view addSubview:firstWordLabel];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGRect newRect = firstWordLabel.frame;
newRect.origin.x = floor(screenWidth/2);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.0];
firstWordLabel.frame = newRect;
[UIView commitAnimations];
Don't u know use of beginAnimations, commitAnimations are discouraged after iOS 4 and later see this so use block based animation methods instead for example
u can use like below
try this hope this might helps
//first create a view which holds the label for example
UIView *holderView = [[UIView alloc]initWithFrame:CGRectMake(x, 100, 10, 35)];
WordLabel *firstWordLabel = [[WordLabel alloc] initWithFrame:holderView.bounds andWithWord:firstWord];
firstWordLabel.text = firstWord.word;
[firstWordLabel sizeToFit];
firstWordLabel.userInteractionEnabled = YES;
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:#selector(labelDragged:)];
[holderView addGestureRecognizer:gesture];
[holderView addSubview:firstWordLabel];
[self.view addSubview:holderView];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGRect newRect = firstWordLabel.frame;
newRect.origin.x = floor(screenWidth/2);
//add the block based animation with option UIViewAnimationOptionAllowUserInteraction
[UIView animateWithDuration:2.0f delay:0.0f options:UIViewAnimationOptionAllowUserInteraction animations:^{
holderView.frame = newRect;
} completion:^(BOOL finished) {
NSLog(#"completed");
}];
for what i did like below try it in new project for testing
- (void)viewDidLoad
{
UIView *holderView = [[UIView alloc]initWithFrame:CGRectMake(20, 100, 50, 35)];
firstWordLabel = [[UILabel alloc]initWithFrame:holderView.bounds]; //is a member variable
firstWordLabel.backgroundColor = [UIColor greenColor];
firstWordLabel.text = #"Hello";
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:#selector(labelDragged:)];
[holderView addGestureRecognizer:gesture];
firstWordLabel.userInteractionEnabled = YES;
[holderView addSubview:firstWordLabel];
[self.view addSubview:holderView];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self startTheAnimation]; //calling animation
}
- (void)startTheAnimation
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGRect newRect = firstWordLabel.frame;
newRect.origin.x = floor(screenWidth/2);
[UIView animateWithDuration:20.0f delay:0.0f options:UIViewAnimationOptionAllowUserInteraction animations:^{
firstWordLabel.frame = newRect;
} completion:^(BOOL finished) {
NSLog(#"completed");
}];
}
- (void)labelDragged:(id)sender
{
NSLog(#"Dragged");
}

UIscrollview and uibuttons tap

I am using a UIScrollView where I got some UIButtons, to recognize that I pressed the button above, I created a subclass of UIScrollView where rewrite the method
- (BOOL) touchesShouldCancelInContentView (UIView *) view
but only enters that method when the touch is a journey, never comes when I press on the screen without more.
the UIScrollView I've created thus:
`
CGSize containerSize = CGSizeMake(320.0f, 480.0f);
CGSize containerSize2 = CGSizeMake(640.0f, 960.0f);
self.scrollView = [[UIScrollViewWithButtons alloc] initWithFrame:(CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=containerSize}];
[self.scrollView setMinimumZoomScale:0.5f];
[self.scrollView setMaximumZoomScale:1.0f];
[self.scrollView setZoomScale:0.5f];
[self.scrollView setDelegate:self];
self.scrollView.bounces = NO;
self.scrollView.bouncesZoom = NO;
self.scrollView.delaysContentTouches = NO;
self.scrollView.userInteractionEnabled = YES;
[self.scrollView setContentInset:UIEdgeInsetsZero];
[self.view addSubview:self.scrollView];
self.containerView = [[UIView alloc] initWithFrame:(CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=containerSize2}];
self.containerView.userInteractionEnabled = YES;
[self.scrollView addSubview:self.containerView];
[self.scrollView setClipsToBounds:NO];
//instanciar uimageview
fondo = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 640, 960)];
[self.containerView addSubview:fondo];
[fondo setImage:[UIImage imageNamed:#"Grande.png"]];
self.scrollView.contentSize = containerSize2;
and button add
imagen = [UIButton buttonWithType:UIButtonTypeCustom];
[imagen addTarget:self action:#selector(pulsadoIzq) forControlEvents:UIControlEventTouchUpInside];
[imagen setTitle:#"" forState:UIControlStateNormal];
[imagen setFrame:CGRectMake(xUser, yUser, 50.0f, 50.0f)];
[imagen setImage:[UIImage imageNamed:#"boton.png"] forState:UIControlStateNormal];
[self.containerView addSubview:fichaUserIzq];
imagen.center = CGPointMake(xUser, yUser);`enter code here`
I need to capture the tap on the screen to the buttons
How I can do?
I would also like to know because if I have implemented this code:
img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"img.png"]];
UIPanGestureRecognizer *panRecg = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(imgDragged:)];
img.userInteractionEnabled = YES;
[img addGestureRecognizer:panRecg];
img.center = CGPointMake(160, 436);
[self.scrollView addSubview:img];
When I click on the image nothing happens, does not enter the method, only comes when I press and drag. I also want to capture when pressed over this image and so I capture only when the move.
I solved the problem, I implement this method:
-(void)singleTapGestureCaptured:(UITapGestureRecognizer*)gesture{
CGPoint touchPoint = [gesture locationInView:self.scrollView];
if ((touchPoint.x > 88)&&(touchPoint.x < 226)&&(touchPoint.y > 172)&&(touchPoint.y < 319)) {
if (rodando) {
[self pararAnimacionDado];
}
}
}
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured:)];
[scrollView addGestureRecognizer:singleTap];

Delete function doesn't works when animation begins ?

I am trying to implement ,long press and delete functionality of apple, myself.
I almost achieved but, there is something i could not figure out.
When shaking animation starts delete button do not take the touches.
How to handle while it is shaking deleting the view.
Here is my code;
self.frame = CGRectMake(0, 0, 1024, 768);
[self setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.8]];
circleView = [[UIView alloc] initWithFrame:CGRectMake(50,55,90,90)];
circleView.layer.cornerRadius = 45;
circleView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];
circleView.layer.borderColor = [[UIColor blackColor] CGColor];
circleView.layer.borderWidth = 4;
UILabel* circleIndex = [[UILabel alloc] init];
circleIndex.frame = CGRectMake(30, 25, 40, 40);
[circleIndex setFont:[UIFont fontWithName:#"Arial-BoldMT" size:40]];
[circleIndex setText:#"A"];
[circleView addSubview:circleIndex];
UIView *exitView = [[UIView alloc] initWithFrame:CGRectMake(78,5,20,20)];
exitView.layer.cornerRadius = 10;
exitView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:1];
exitView.layer.borderColor = [[UIColor whiteColor] CGColor];
exitView.layer.borderWidth = 2;
UILabel* exitLabel = [[UILabel alloc] init];
exitLabel.frame = CGRectMake(5, 0.5, 25, 20);
[exitLabel setFont:[UIFont fontWithName:#"Arial-BoldMT" size:25]];
[exitLabel setTextColor:[UIColor whiteColor]];
[exitLabel setBackgroundColor:[UIColor clearColor]];
[exitLabel setText:#"-"];
[exitView addSubview:exitLabel];
[circleView addSubview:exitView];
UIView *alertView = [[UIView alloc]initWithFrame:CGRectMake(40, 80, 944, 600)];
[exitView setUserInteractionEnabled:YES];
UILongPressGestureRecognizer *longPress =
[[UILongPressGestureRecognizer alloc] initWithTarget:self
action:#selector(handleLongPress:)];
[circleView addGestureRecognizer:longPress];
UITapGestureRecognizer *singlePress =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSinglePress:)];
[exitView addGestureRecognizer:singlePress];
//[longPress release];
[circleView bringSubviewToFront:exitView];
[alertView addSubview:circleView];
[self addSubview:alertView];
}
return self;
}
//The event handling method
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer{
//CGPoint location = [recognizer locationInView:[recognizer.view superview]];
if ( recognizer.state == UIGestureRecognizerStateEnded ) {
//[circleView removeFromSuperview];
[self shakeView:recognizer.view];
}
}
- (void)handleSinglePress:(UITapGestureRecognizer *)recognizer{
//CGPoint location = [recognizer locationInView:[recognizer.view superview]];
if ( recognizer.state == UIGestureRecognizerStateEnded ) {
[recognizer.view.superview removeFromSuperview];
}
}
- (void)shakeView:(UIView *)viewToShake
{
CGFloat t = 2.0;
CGAffineTransform translateRight = CGAffineTransformTranslate(CGAffineTransformIdentity, t, 0.0);
CGAffineTransform translateLeft = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, 0.0);
viewToShake.transform = translateLeft;
[UIView animateWithDuration:0.07 delay:0.0 options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat animations:^{
[UIView setAnimationRepeatCount:200.0];
viewToShake.transform = translateRight;
} completion:^(BOOL finished) {
if (finished) {
[UIView animateWithDuration:0.05 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
viewToShake.transform = CGAffineTransformIdentity;
} completion:NULL];
}
}];
}
Use in the options parameter of your animation UIViewAnimationOptionAllowUserInteraction

How to fit image in UITextView and Tap to get larger

I have created a Table-view in main ViewController once i touch the row it will take to the next view controller(DetailViewController) where it display the image and name of dish.And once i tap the image it should get larger but it is not getting bigger .The below code is which i have tried in DetailViewController.
DetailViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
imageView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10,100, 100)];
CGRect textViewFrame = CGRectMake(10, 10, 300, 400);
textView = [[UITextView alloc] initWithFrame:textViewFrame];
textView.returnKeyType = UIReturnKeyDone;
textView.backgroundColor=[UIColor whiteColor];
textView.editable=NO;
textView.delegate = self;
[self.view addSubview:textView];
[textView addSubview:imageView];
textView.alpha = 0.9;
textView.font = [UIFont systemFontOfSize:15];
if(mrowno==0)
{
imageView.image=[UIImage imageNamed:#"Dosa.jpg"];
textView.text = #"\n\n\n\n\n\n\n\nDOSA\nDosa, ";
imageButton = [[UIButton alloc]init];
[imageButton setFrame:CGRectMake(0, 0, 100, 100)];
[imageButton addTarget:self action:#selector(imageTapped:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:imageButton];
}
Here is the method to make image larger
-(void)imageTapped:(UIButton*)in_sender
{
[UIView transitionWithView:in_sender.superview duration:0.8f options:UIViewAnimationOptionCurveLinear animations:^
{
[in_sender.superview setFrame:CGRectMake(0, 0, 300, 500)];
}
completion:^(BOOL finished)
{
}];
}
You can increase and decrease the size of image by pinch in and pinch out,If it required
-(void)viewDidLoad
{
UIPinchGestureRecognizer* pinch=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinching:)];
[self.imageView addGestureRecognizer:pinch];
}
-(void)pinching:(UIGestureRecognizer*) recognizer
{
UIPinchGestureRecognizer* pinch=(UIPinchGestureRecognizer*) recognizer;
self.imageView.transform=CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
pinch.scale=1;
NSLog(#"pinching");
}
You do not want to use transitionWithView. Instead, you want to use + animateWithDuration: delay:options:animations:completion: (or any of its shorter variants...)
[UIView animateWithDuration:0.3f delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
viewYouWantToAnimate.frame = CGRectMake(..., ..., ..., ...);
} completion:^{
//Add completion code here, or pass nil if you don't have anything to do.
}];
Edit: In case you were wondering what transitionWithView was used for... the AppleDocs have a great example of it adding animations for remove/addSubview methods (not frame modifications):
[UIView transitionWithView:containerView
duration:0.2
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[fromView removeFromSuperview];
[containerView addSubview:toView];
} completion:NULL];

uibutton is not getting displayed

I am trying to add uibutton inside the imageview.
But it was not displayed the button.
Even i tried adding it to the uiscrollview and also for self.view.
But nothing were displayed the uibutton
Pls let me know what is the problem
const CGFloat HEIGHT = 1024.0;
const CGFloat WIDTH = 768.0;
#define myViewPortrait CGRectMake(0.0f, 0.0f, 768.0f,1024.0f)
#define myViewLandSacpe CGRectMake(0.0f, 0.0f, 1024.0f,768.0f)
#define kAnimationKey #"animationKey"
-(void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [myScrollView subviews];
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (self.view.frame.size.width);
}
}
[myScrollView setContentSize:CGSizeMake((myImagesCount * self.view.frame.size.width), [myScrollView bounds].size.height)];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
myScrollView = [[UIScrollView alloc] initWithFrame:
CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:myScrollView];
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
rightRecognizer.numberOfTouchesRequired = 1;
[rightRecognizer setDelegate:self];
[myScrollView addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(leftSwipeHandle:)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
leftRecognizer.numberOfTouchesRequired = 1;
[leftRecognizer setDelegate:self];
[myScrollView addGestureRecognizer:leftRecognizer];
[leftRecognizer release];
[myScrollView setBackgroundColor:[UIColor blackColor]];
[myScrollView setCanCancelContentTouches:NO];
myScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
myScrollView.clipsToBounds = YES;
myScrollView.scrollEnabled = YES;
myScrollView.pagingEnabled = YES;
myScrollView.delegate = self;
myImagesCount = 5;
myScrollView.showsHorizontalScrollIndicator=NO;
myScrollView.showsVerticalScrollIndicator=NO;
for (int i = 1; i <= myImagesCount; i++)
{
NSString *imageName = [NSString stringWithFormat:#"screen-%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = myScrollView.frame.size.height;
NSLog(#"%d -----",self.view.frame.size.width);
rect.size.width = myScrollView.frame.size.width;
imageView.frame = rect;
imageView.tag = i;
[myScrollView addSubview:imageView];
[imageView release];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
[button setTitle:#"point" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 100.0, 40.0);
button.backgroundColor = [UIColor blackColor];
[self.view addSubview:button];
[self layoutScrollImages];
[super viewDidLoad];
}
Based off your question, you'd like to add the UIButton as a subview to the UIImageView - I'm guessing in the for loop? Immediate problem I see is that the actual button is being generated outside of the for loop. I think your for loop is intended to look like this:
for (int i = 1; i <= myImagesCount; i++)
{
NSString *imageName = [NSString stringWithFormat:#"screen-%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
[button setTitle:#"point" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 100.0, 40.0);
button.backgroundColor = [UIColor blackColor];
CGRect rect = imageView.frame;
rect.size.height = myScrollView.frame.size.height;
NSLog(#"%d -----",self.view.frame.size.width);
rect.size.width = myScrollView.frame.size.width;
imageView.frame = rect;
imageView.tag = i;
[imageView addSubview:button];
[myScrollView addSubview:imageView];
[imageView release];
}
Try this
Declare button globally and set
[self layoutScrollImages];
[self.view bringSubViewToFront:button];

Resources