iOS - UILabels and Gestures - ios

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");
}

Related

Subview not moving along with its superview

I've done this a million times before but finding myself confused now.
When you animate a container view to change its position, its subviews move along with it don't they? You don't change subviews' frames you just change superview's frame because all subviews' positions inside their superview don't change.
But for some reason this time the subviews would not move along with their superview, it'd stay stuck at its original position.
- (void)writeCommentTapped{
UIView *writeCommentView = [[UIView alloc]init];
writeCommentView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview: writeCommentView];
[self.view bringSubviewToFront:self.navView];
self.writeCommentTextView = [[UITextView alloc]init];
self.writeCommentTextView.backgroundColor = [UIColor blackColor];
[writeCommentView addSubview:self.writeCommentTextView];
[self.writeCommentTextView becomeFirstResponder];
self.writeCommentTextView.returnKeyType = UIReturnKeyDefault;
writeCommentView.frame = CGRectMake(0, -self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - self.keyboardSize.height);
self.writeCommentTextView.frame = CGRectMake(0, 0, writeCommentView.frame.size.width - 40, writeCommentView.frame.size.height - 100);
self.writeCommentTextView.center = writeCommentView.center;
[UIView animateWithDuration:0.15
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
writeCommentView.frame = CGRectMake(0, 0, writeCommentView.frame.size.width, writeCommentView.frame.size.height);
} completion:^(BOOL finished){
if (finished) {
}
}
];
}
Remove self.writeCommentTextView.center and set calculated frame for center
-(void)writeCommentTapped{
UIView *writeCommentView = [[UIView alloc]init];
writeCommentView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview: writeCommentView];
[self.view bringSubviewToFront:self.navView];
self.writeCommentTextView = [[UITextView alloc]init];
self.writeCommentTextView.backgroundColor = [UIColor blackColor];
[writeCommentView addSubview:self.writeCommentTextView];
[self.writeCommentTextView becomeFirstResponder];
self.writeCommentTextView.returnKeyType = UIReturnKeyDefault;
writeCommentView.frame = CGRectMake(0, -self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - self.keyboardSize.height);
writeCommentView.frame = CGRectMake(0, -self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height);
self.writeCommentTextView.frame = CGRectMake(40/2, 100/2, writeCommentView.frame.size.width - 40, writeCommentView.frame.size.height - 100);
// self.writeCommentTextView.center = writeCommentView.center;
[UIView animateWithDuration:0.15
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
writeCommentView.frame = CGRectMake(0, 0, writeCommentView.frame.size.width, writeCommentView.frame.size.height);
} completion:^(BOOL finished){
if (finished) {
}
}
];
}
OR
self.writeCommentTextView.center = CGPointMake(writeCommentView.frame.size.width/2.0, writeCommentView.frame.size.height/2.0);

Swipe Subview Off Screen

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.

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 add subview with flip animation?

If you create a brand new single view app and put this code behind a button:
UIView *blah = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
blah.backgroundColor = [UIColor grayColor];
[UIView transitionWithView:blah duration:1
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
[self.view addSubview:blah];
}
completion:^(BOOL finished){
}];
The subview is added immediately with no animation. If you add the subview first then try to animate it... you get the same problem.
UIView *blah = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
[self.view addSubview:blah];
[UIView transitionWithView:blah duration:1
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
blah.backgroundColor = [UIColor grayColor];
}
completion:^(BOOL finished){
}];
How on Earth do you animate a flip for a subview while or immediately after adding it?
You generally need to have the container that constrains the animation to be in place already:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, 100, 100);
_container = [[UIView alloc] initWithFrame:frame];
_container.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:_container];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIView *subview = [[UIView alloc] initWithFrame:_container.bounds];
subview.backgroundColor = [UIColor darkGrayColor];
[UIView transitionWithView:_container
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
[_container addSubview:subview];
}
completion:NULL];
}
This is worth a try:
UIView *blah = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
blah.backgroundColor = [UIColor grayColor];
[self.view addSubview:blah];
blah.alpha = 0.0; //Or with blah.hidden = TRUE and then FALSE inside the animation block
[UIView transitionWithView:blah
duration:1
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
blah.alpha = 1.0;
}
completion:^(BOOL finished){
}];

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];

Resources