I have an object created from a class of type UIView to produce an unlimited number of views which then appear in smaller sizes in the middle of the screen. I used UITapGestureRecognizer to enlarge one of the views to fit the screen but the issue here is that the object is only usable within the first class and cannot be transferred to the second class which is the selector.
What are your suggestions regarding this particular issue?
How can I send my object to the selector to be able to use it there?
Thank you
Here is the first class which creates the object (derived from iCarousel) :
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UILabel *label = nil;
UIButton *close = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//create new view if no view is available for recycling
if (view == nil)
{
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320.0f, 460.0f)];
view.contentMode = UIViewContentModeCenter;
view.backgroundColor = [UIColor whiteColor];
label = [[UILabel alloc] initWithFrame:CGRectMake(100, -100, 100, 100)];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [label.font fontWithSize:50];
label.tag = 1;
[view addSubview:label];
close.frame = CGRectMake(0, 0, 30, 30);
[close setTitle:#"x" forState:UIControlStateNormal];
close.titleLabel.font = [UIFont systemFontOfSize:25];
[close addTarget:self action:#selector(closeMe:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:close];
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapToMaximize:)];
tap.numberOfTapsRequired = 1;
[view addGestureRecognizer:tap];
}
else
{
//get a reference to the label in the recycled view
label = (UILabel *)[view viewWithTag:1];
}
label.text = [items[index] stringValue];
return view;
}
And the selector:
- (void)tapToMaximize:(UITapGestureRecognizer*)recognizer {
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionCurveLinear
animations:^{
carousel.transform = CGAffineTransformMakeScale(1, 1);
}
completion:^(BOOL finished) {
}];
}
So instead of using "carousel" which represents all the views, I want to use "view" so that I can only enlarge the current view and not all of them.
UITapGestureRecognizer inherits the view property from UIGestureRecognizer, which you can use to enlarge just that tap gesture recognizer's view:
- (void)tapToMaximize:(UITapGestureRecognizer*)recognizer {
UIView *viewToMaximize;
if ([recognizer.view isEqual:_maximizedView]) {
viewToMaximize = nil;
} else {
viewToMaximize = recognizer.view;
}
UIView *viewToUnmaximize = _maximizedView; // _maximizedView should be an instance variable on your view controller
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionCurveLinear
animations:^{
viewToMaximize.transform = CGAffineTransformMakeScale(1.1, 1.1);
viewToUnmaximize.transform = CGAffineTransformIdentity;
}
completion:^(BOOL finished) {
}];
_maximizedView = viewToMaximize;
}
Also, CGAffineTransformMakeScale(1, 1) will scale the view to its original size, so I bumped the scale up in my code snippet.
If I'm understanding this correctly you want to transform the UIView that you tapped on, which should be the carousel?
Assuming you added the tap gesture recognizer to self.view, then this is how you get the view you just tapped on:
- (void)tapToMaximize:(UITapGestureRecognizer*)recognizer
{
UIView *view = [self.view hitTest:[recognizer locationInView:self.view] withEvent:nil];
// you can now check to see if this view is the carousel
}
Once you know whether or not you tapped on the carousel, you can store it, pass it on to another class, transform it, or anything you like.
Related
This question already has answers here:
How to stop unwanted UIButton animation on title change?
(24 answers)
Closed 4 months ago.
I am animating a view that is sliding down. In the view there is a simple UIButton. The view and the button have fixed widths and heights (I checked with adding a color as background). Although when use:
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//Slide Down the notification
CGRect notificationsRect = self.notificationContainerView.bounds;
notificationsRect.origin.y = 0;
notificationViewController.view.frame = notificationsRect;
} completion:^(BOOL finished) {
[notificationViewController didMoveToParentViewController:self];
self.currentNotification = notificationViewController;
}];
then the view slides down perfectly, expect for that the text in the UIButton kind of fades in. It starts really small and animates to the correct font size.
How can I disable the animation of the text in the UIButton?
Is the effect you have something similar to what is below? I tried to imitate your situation by having a view with a UIButton inside with some some text set for the title of the button. I then animated the button similarly to the way you did. As you can see there isn't really a fade happening on the text of my button inside my view that is sliding so I wonder if there is something else at play for you. I have the code I used below also to mock the situation.
- (void)viewDidLoad {
[super viewDidLoad];
self.myView = [[UIView alloc] initWithFrame:CGRectMake(200, 0, 100, 100)];
self.myView.backgroundColor = [UIColor greenColor];
UIButton *myButton= [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 80, 80)];
myButton.backgroundColor = [UIColor blueColor];
[myButton setTitle:#"fun times" forState:UIControlStateNormal];
[self.myView addSubview:myButton];
[self.view addSubview:self.myView];
UIButton *resetButton = [[UIButton alloc] initWithFrame:CGRectMake(300, 300, 50, 50)];
resetButton.backgroundColor = [UIColor blackColor];
[resetButton addTarget:self action:#selector(reset) forControlEvents:UIControlEventTouchUpInside];
UIButton *goButton = [[UIButton alloc] initWithFrame:CGRectMake(300, 100, 50, 50)];
goButton.backgroundColor = [UIColor redColor];
[goButton addTarget:self action:#selector(go) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:goButton];
[self.view addSubview:resetButton];
}
- (void)reset {
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//Slide Up the notification
CGRect notificationsRect = self.myView.bounds;
notificationsRect.origin.y = 0;
notificationsRect.origin.x = 200;
self.myView.frame = notificationsRect;
} completion:nil];
}
- (void)go {
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//Slide Down the notification
CGRect notificationsRect = self.myView.bounds;
notificationsRect.origin.y = 200;
notificationsRect.origin.x = 200;
self.myView.frame = notificationsRect;
} completion:nil];
}
Disable animations for UIButton using setAnimationsEnabled:(BOOL)enabled
Reference:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/occ/clm/UIView/setAnimationsEnabled:
I am working on a UITableView which has a SwipeGesture.
On Swipe, the Cell should Swipe Left and the objects which are set at 320 should appear
Place them in the contentView of the UITableViewCell and make an animation on the contentView that changes the frame to x = -320 and it will work.
UITableView change the frame of cells when they are added to the table view. If you want to change the width of a cell, you should either change the width of the table, or change the width of the contentView in the cell.
A better way to achieve this is subclassing UITableViewCell and overriding its -setFrame: method like this:
Try this one:
- (void)setFrame:(CGRect)frame
{
frame.origin.x += inset;
frame.size.width -= 2 * inset;
[super setFrame:frame];
}
Try the below code.
// Create the swipe view
//Change the frame as per your requirement
UIView* _swipeView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, 10, 70)];
//setting the background
[_swipeView setBackgroundColor:[UIColor redColor]];
[_swipeView setTag:1896];
[cell.contentView addSubview:_swipeView];
// Create the gesture recognizers
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipeRightInCell:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipeLeftInCell:)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[cell addGestureRecognizer:swipeRight];
[cell addGestureRecognizer:swipeLeft];
>
-(void) didSwipeRightInCell:(UISwipeGestureRecognizer*)ges{
UITableViewCell* cellViewInProgress_ = (UITableViewCell*)ges.view;
for(UIView* view in cellViewInProgress.contentView.subviews)
{
if(view.tag == 1896){
[UIView animateWithDuration:0.5 animations:^{
//Change the frame as per your requirement
view.frame = CGRectMake(5, 5, 220, 70);
}];
// I have placed UILabel you can try your own control
//Change the frame as per your requirement
UILabel *lblDemo = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 200, 30)];
[lblDemo setFont:[Support getAppFontSemiBold:16.0]];
[lblDemo setTextColor:[UIColor whiteColor]];
[lblDemo setBackgroundColor:[UIColor clearColor]];
lblDemo.tag = 2341;
[lblDemo setText:#"Hello"]];
[view addSubview:lblDemo];
}
}
}
>
-(void) didSwipeLeftInCell :(UISwipeGestureRecognizer*)ges{
UITableViewCell* cellViewInProgress_ = (UITableViewCell*)ges.view;
for(UIView* view in cellViewInProgress.contentView.subviews)
{
if(view.tag == 1896){
UILabel* lbl = (UILabel*)[view viewWithTag:2341];
[UIView animateWithDuration:0.5 animations:^{
//Change the frame as per your requirement
view.frame = CGRectMake(5, 5, 10, 70);
[lbl removeFromSuperview];
}];
}
}
}
Hope this helps.
I am trying to trigger adding two subviews when a Bar Button is tapped. But adding the subviews works just fine, but when I try removing the subviews, it doesn't work.
Here is the code I am implementing
-(IBAction)showPopover:(id)sender{
UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
UIView *popoverViewBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1000)];
popoverView.alpha = 0.0;
popoverView.layer.cornerRadius = 2;
popoverView.layer.borderWidth = 0.1f;
popoverView.layer.backgroundColor = [UIColor whiteColor].CGColor;
popoverView.layer.masksToBounds = YES;
popoverViewBackground.layer.backgroundColor= [UIColor blackColor].CGColor;
if (popoverCount == 0) {
[self.view addSubview:popoverViewBackground];
[self.view addSubview:popoverView];
popoverCount = 1;
}else if (popoverCount ==1){
[popoverView removeFromSuperview];
[popoverViewBackground removeFromSuperview];
popoverCount = 0;
}
[popoverViewBackground setAlpha:0.5];
[popoverView setAlpha:1.0];
}
Problem is that you are creating new view every time you click the button so old view not removing put your code like thi, then it will work fine.I have tested it.
in .h file
#interface secondViewController : UIViewController
{
int popoverCount;
UIView *popoverView ;
UIView *popoverViewBackground;
}
in .m file
- (void)viewDidLoad
{
[super viewDidLoad];
popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, -100, 320, 100)];
popoverViewBackground = [[UIView alloc] initWithFrame:CGRectMake(0, -100, 320, 100)];
popoverView.alpha = 0.0;
popoverView.layer.cornerRadius = 2;
popoverView.layer.borderWidth = 0.1f;
popoverView.layer.backgroundColor = [UIColor whiteColor].CGColor;
popoverView.layer.masksToBounds = YES;
popoverViewBackground.layer.backgroundColor= [UIColor blackColor].CGColor;
}
-(IBAction)showPopover:(id)sender {
if (popoverCount == 0) {
[self.view addSubview:popoverViewBackground];
[self.view addSubview:popoverView];
[UIView animateWithDuration:0.5
animations:^{
popoverView.frame = CGRectMake(0,0,320,100);
}
completion:^(BOOL finished){
;
}];
[UIView animateWithDuration:0.5
animations:^{
popoverViewBackground.frame = CGRectMake(0,0,320,100);
}
completion:^(BOOL finished){
;
}];
popoverCount = 1;
}else if (popoverCount ==1){
[UIView animateWithDuration:0.5
animations:^{
popoverView.frame = CGRectMake(0,-100,320,100);
}
completion:^(BOOL finished){
[popoverView removeFromSuperview];
}];
[UIView animateWithDuration:0.5
animations:^{
popoverViewBackground.frame = CGRectMake(0,-100,320,100);
}
completion:^(BOOL finished){
[popoverViewBackground removeFromSuperview];
}];
popoverCount = 0;
}
[popoverViewBackground setAlpha:0.5];
[popoverView setAlpha:1.0];
}
UIView *popoverView;
UIView *popoverViewBackground;
Declare popoverView and popoverViewBackground in .h file and allocate and initializes of these subview in when popoverCount is zero.
-(IBAction)showPopover:(id)sender{
if (popoverCount == 0) {
popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
popoverViewBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1000)];
popoverView.alpha = 0.0;
popoverView.layer.cornerRadius = 2;
popoverView.layer.borderWidth = 0.1f;
popoverView.layer.backgroundColor = [UIColor whiteColor].CGColor;
popoverView.layer.masksToBounds = YES;
popoverViewBackground.layer.backgroundColor= [UIColor blackColor].CGColor;
[self.view addSubview:popoverViewBackground];
[self.view addSubview:popoverView];
popoverCount = 1;
}else if (popoverCount ==1){
[popoverView removeFromSuperview];
[popoverViewBackground removeFromSuperview];
popoverCount = 0;
}
[popoverViewBackground setAlpha:0.5];
[popoverView setAlpha:1.0];
}
At least 2 issues in your code:
Every time the method is involved, you are creating new instances of these sub-views
You are removing the newly created instances. That is, previous instances that were added still exist.
You should
save these sub-views in instance variables, so that you can refer to them and remove them correctly.
move the creation code inside the first if-block, so that you are not creating too many sub-views.
It is happening because you are applying addSubview and removeFromSuperview on different object.
When First time showPopover is called it create two object of UIView
with name popoverView and popoverViewBackground.
and add this to self.view.
Till now every thing fine but when this method call second time
New object of popoverView and popoverViewBackground created again and
You are trying to remove that new created object from self.view which is not there.
How to solve this:
There are two way to solve this:
1) Create this object in .h file so you can access it from anywhere.
2) create object in such way that it created only when method call first time.
Give this UIView tag and when you remove find this tag in UIView.subview
Way 1 is easy way 2 require some code but you can make it efficient in turm of memory.
All the best
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];
I have a custom subclassed uiview I created to display in-app notifications.
I can call the view just fine, but am having problems dismissing it using a uibutton (embedded in the custom view)
When the button is pressed, the app crashes and I get this error:
UPDATE - Fixed the above issue, but now only the button dismisses, and not the actual view. See updated code below.
-(id)initWithMessage:(NSString *)message{
self = [super initWithFrame:CGRectMake(0, -70, 320, 60)];
if (self) {
//Add Image
UIImage *image = [UIImage imageNamed:#"notice-drop-down"];
UIImageView *background = [[UIImageView alloc] initWithImage:image];
[self addSubview:background];
//Add Label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, self.frame.size.height/2-25, 300, 50)];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor blackColor]];
[label setText:message];
label.numberOfLines = 0;
[label setFont:[UIFont fontWithName:#"Hand of Sean" size:16]];
//NSLog(#"FONT FAMILIES\n%#",[UIFont familyNames]);
[self addSubview:label];
//Add Close Button
UIButton *closeButton = [[UIButton alloc] initWithFrame:CGRectMake(280, self.frame.size.height/2-15, 30, 30)];
UIImage *closeImage = [UIImage imageNamed:#"notice-close"];
[closeButton setImage:closeImage forState:UIControlStateNormal];
[closeButton addTarget:self action:#selector(closeNoticeDropDown:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:closeButton];
//Animate In
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationCurveEaseIn
animations:^{
self.frame = CGRectMake(0,70,320,60);
}
completion:nil
];
}
return self;
}
-(void)closeNoticeDropDown:(id)self{
NoticeDropDown *notice = (NoticeDropDown *)self;
NSLog(#"Frame: %f",notice.frame.size.width);
//Animate In
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationCurveEaseOut
animations:^{
notice.frame = CGRectMake(0,-70,320,60);
}
completion:^(BOOL finished){
[notice removeFromSuperview];
//notice = nil;
}
];
}
View call from another view controller:
noticeDropDown = [[NoticeDropDown alloc] initWithMessage:message];
[self.view insertSubview:noticeDropDown belowSubview:hudContainerTop];
You have declared your method to be a class method but are sending the message to an instance. If you want it to be a class method still, pass [NoticeDropDown class] as the target parameter to your addTarget:action:forControlEvemts: method. Otherwise replace the "+" with a "-" in the method declaration.
Also- when UIControl actions have sender parameters it will send the control as the sender - so you will get a UIButton instead of your view.
My recommendation is to change your action to an instance method and replace "sender" with "self".
I apologize for formatting I'm posting from my phone. I'll try to fix when back at a computer.
EDIT:
Change your updated method like so:
-(void)closeNoticeDropDown:(id)sender{
NSLog(#"Frame: %f",notice.frame.size.width);
//Animate In
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationCurveEaseOut
animations:^{
self.frame = CGRectMake(0,-70,320,60);
}
completion:^(BOOL finished){
[self removeFromSuperview];
}
];
}
You don't need to pass self into a method, it always exists.
You probably try to call your method on your view instance (something like [noticeDropDown closeNoticeDropDown:...]) but closeNoticeDropDown: is a class method and you should call it this way:
[NoticeDropDown closeNoticeDropDown: noticeDropDown];
There's also few things in your animation code that look wrong:
[UIView commitAnimations]; calls should be removed as they are used in pair with [UIView beginAnimations:... context:...] method, they're not required with block-based animation
[sender removeFromSuperview]; call should go in animation's completion block, otherwise it will be called before your animation is even started and you won't get desired effect