Unwanted animations coming from nowhere - ios

In my iOS app, I have some animations that seem to be coming out of nowhere. They don't happen at first, only after using the app a little. Here is a Youtube video with a better description:
http://www.youtube.com/watch?v=whfG67EP6kk
I'm not really sure why the elements are animating the way they are (or at all). The code I'm calling is just a simple [view addSubView:viewname]. Any help as to how this is happening and how to prevent it would be great.
EDIT
Here is the code for the text that comes flying in from seemingly all directions:
-(void)displayPublicDataForEntry:(PFObject *)entry likes:(unsigned long)likesCount comments:(unsigned long)commentsCount
{
[indicator stopAnimating];
[indicator removeFromSuperview];
[zippingLabel removeFromSuperview];
entryStatus.text = #"This entry is public";
entryStatus.backgroundColor = [UIColor clearColor];
entryStatus.frame = CGRectMake(10, 3, 200, 20);
[shareView addSubview:entryStatus];
// Display a UILabel of number of likes
if (likesCount == 1)
numberOfLikes.text = [NSString stringWithFormat:#"%lu like", likesCount];
else
numberOfLikes.text = [NSString stringWithFormat:#"%lu likes", likesCount];
CGSize stringsize = [numberOfLikes.text sizeWithFont:[UIFont systemFontOfSize:14]];
numberOfLikes.frame = CGRectMake(10, 22, stringsize.width, 20);
numberOfLikes.backgroundColor = [UIColor clearColor];
[shareView addSubview:numberOfLikes];
// Display a UILabel of number of comments
if (commentsCount == 1)
numberOfComments.text = [NSString stringWithFormat:#" / %lu comment", commentsCount];
else
numberOfComments.text = [NSString stringWithFormat:#" / %lu comments", commentsCount];
numberOfComments.frame = CGRectMake(numberOfLikes.frame.size.width + 10, 22, 100, 20);
numberOfComments.backgroundColor = [UIColor clearColor];
[shareView addSubview:numberOfComments];
viewEntry.frame = CGRectMake(self.view.frame.size.width - 95, 12, 100, 40);
[viewEntry addTarget:self
action:#selector(viewPublicEntry)
forControlEvents:UIControlEventTouchUpInside];
[shareView addSubview:viewEntry];
}
There's nothing in there (at least I don't think there is) that would cause these strange animations.

I had this problem. The cause ended up being a call to begin animations without a call to commit. I somehow called [UIView beginAnimations:Nil context:nil] twice.

Related

UIview.hidden doesn't disappear but hides behind another view

So I am trying to hide a UIView but something weird is happening. After using this line of code:
ntcCircleView.hidden = YES;
The view won't disappear, but it hides behind another UIView.
This is the complete code I use:
UIView* NtcContainer=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 40-17, 3, 40, 40)];
UIView* NtcView=[[UIView alloc]initWithFrame:CGRectMake(5, 0, 40, 40)];
notificationButton = [ZFRippleButton buttonWithType:UIButtonTypeCustom];
notificationButton.frame = CGRectMake(0, 0, 40, 40);
notificationButton.layer.cornerRadius=menuButton.frame.size.width/2;
[notificationButton addTarget:self action:#selector(goToNotificationsList:) forControlEvents:UIControlEventTouchUpInside];
notificationImage=[[UIImageView alloc]initWithFrame:CGRectMake(10, 12, 20, 20)];
notificationImage.image=[UIImage imageNamed:[HotelStay sharedInstance].icon.Notification];
ntcCircleView = [[UIView alloc] initWithFrame:CGRectMake(20,5,16,16)];
ntcCircleView.alpha = 0.7;
ntcCircleView.layer.cornerRadius = ntcCircleView.frame.size.width/2; // half the width/height
ntcCircleView.backgroundColor = [UIColor redColor];
ntcNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,16,16)];
ntcNumberLabel.textAlignment = NSTextAlignmentCenter;
[ntcNumberLabel setTextColor:[UIColor whiteColor]];
[ntcNumberLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:11.0]];
[ntcCircleView addSubview:ntcNumberLabel];
int ntcNum = [dataManager getUnreadNotificationNumber];
if (ntcNum==0)
{
ntcCircleView.hidden = YES;
}else
{
ntcNumberLabel.text = [NSString stringWithFormat:#"%i",ntcNum];
}
[NtcView addSubview:notificationImage];
[NtcView addSubview:notificationButton];
[NtcView addSubview:ntcCircleView];
[NtcContainer addSubview:NtcView];
[self.view addSubview:NtcContainer];
So ntcCircleView after hidding it, just goes behind notificationImage.
What drives me crazy, is that I use successfully the exact same code in another Views with the only difference the last line. Instead of using:
[self.view addSubview:NtcContainer];
I add the views to the navigation bar like this:
UIBarButtonItem *ntcBarItem = [[UIBarButtonItem alloc] initWithCustomView:NtcContainer];
self.navigationItem.rightBarButtonItem = ntcBarItem;
What am I missing here ?
UPDATE
I also noticed that this bug happens only when I use
[self.navigationController popViewControllerAnimated:YES];
to navigate back to the view.
You need to update these snippet:
[NtcView addSubview:notificationImage];
[NtcView addSubview:notificationButton];
[NtcView addSubview:ntcCircleView];
[NtcContainer addSubview:NtcView];
to:
[NtcView addSubview:notificationButton];
[NtcView addSubview:ntcCircleView];
[NtcView addSubview:notificationImage];
[NtcContainer addSubview:NtcView];
It will solve your problem.
I figured it out. I was using a deprecated method (viewDidUnload) to release the notification observer.

ActivityView not showing

I am trying to add a download progress view to my app, and while it shows in the debug preview, it does not actually show on the device. Can anyone explain this to me?
Here is a screenshot of the debug and some code:
- (void)updateDownloadProgressWithBatchIndex:(int)index contentName:(NSString *)name
{
CGSize size = self.view.bounds.size;
_loadingView = [[UIView alloc] initWithFrame:CGRectMake(75, 155, 170, 170)];
_loadingView.backgroundColor = [UIColor blackColor];
_loadingView.clipsToBounds = YES;
_loadingView.layer.cornerRadius = 10.0;
[_loadingView setCenter:CGPointMake(size.width/2, size.height/2)];
_activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
_activityView.frame = CGRectMake(65, 40, _activityView.bounds.size.width, _activityView.bounds.size.height);
_activityView.hidden = NO;
[_activityView setCenter:CGPointMake(size.width/2, size.height/2)];
[_loadingView addSubview:_activityView];
_loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 115, 130, 22)];
_loadingLabel.backgroundColor = [UIColor clearColor];
_loadingLabel.textColor = [UIColor whiteColor];
_loadingLabel.adjustsFontSizeToFitWidth = YES;
_loadingLabel.textAlignment = UITextAlignmentCenter;
_loadingLabel.text = [NSString stringWithFormat:#"Batch %i downloading...", index];
[_loadingView addSubview:_loadingLabel];
[self.view addSubview:_loadingView];
_loadingView.hidden = NO;
UIView *v = self.view;
[_activityView startAnimating];
}
I'm going to take a wild guess - it has to be wild, because you refuse to show your code - that you are proceeding to synchronous networking after you call updateDownloadProgress. That's the cause of the issue. The inability of the activity view to start spinning would thus be diagnostic, the real issue being the synchronous networking, which is a no-no.

Responsive label size

I'm working on my first app which is a simple quote generator. I have run into a couple of problems and I'm kind of out of my depth here (the freelancer I hired has done a half-baked job, and I'm trying to fix it myself now that I'm out of cash). Any pointers would be very helpful.
There is a list view of favourited quotes, and the space provided per quote is fixed - which looks odd when the quotes vary in size (there's either lots of empty space or the quote cuts off). I think the snippet below is where this can be amended, as it's where I changed the text alignment.
Also I made some edits to the text file which the app pulls the quotes from, but they are only sometimes reflected when the app is running in the simulator... This seems really strange that some of the edits work and some don't.
Any suggestions highly appreciated as I've tried everything I can think of through trial and error.
UILabel *favouriteLabel;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
favouriteLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, -10, 400, 140)];
}else{
favouriteLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, -20, 160, 100)];
}
favouriteLabel.backgroundColor = [UIColor clearColor];
favouriteLabel.text = [self.arrayFromFile objectAtIndex:indexPath.row];
NSLog(#"Row alloc %d", indexPath.row);
NSLog(#"Load label %#", favouriteLabel.text);
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
favouriteLabel.font = [UIFont fontWithName: #"Eras Bold ITC" size: 18];
}else if([GUIHelper isPhone5]){
favouriteLabel.font = [UIFont fontWithName: #"Dosis-Medium" size: 14];
}
else
favouriteLabel.font = [UIFont fontWithName: #"Dosis-Medium" size: 14];
favouriteLabel.textColor = [UIColor blackColor];
favouriteLabel.textAlignment = UITextAlignmentCenter;
favouriteLabel.numberOfLines = 0;
favouriteLabel.center = CGPointMake(cell.center.x - 40, cell.center.y + 20);
favouriteLabel.tag = 900;
[cell.contentView addSubview:favouriteLabel];
UIImage *lineImg = [UIImage imageNamed:#"quote-divider#2x.png"];
CGRect lineImageFrame;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
lineImageFrame = CGRectMake(0, 110, lineImg.size.width, lineImg.size.height);
}else{
lineImageFrame = CGRectMake(0, 62, lineImg.size.width * 0.9, 0.8*lineImg.size.height);
}
UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:lineImageFrame];
lineImageView.image = lineImg;
lineImageView.center = CGPointMake(cell.center.x - 40, lineImageView.center.y + 35);
// set the tag myImageView.tag = 1010;
[cell.contentView addSubview:lineImageView];

Autoresize UIScrollView To Fit UILabel Text

I have been searching for quite a while now, and every answer I find doesn't seem to work. I have a view, code below, that holds a bunch of text that is pulled from a YouTube video description from another view. Obviously ever video's descriptions are of different length, which is what poses the problem. I want to make it that the scrollview only scrolls down to about 10 pixels after the last text line of the label.
In this code, I don't have it doing that, I have it at a fixed size.
Can ANYONE help? I have been searching for days now, and I can seem to find a proper solution.
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
VideoDescription = [VideoDescription stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//int a = VideoDescription.length;
//NSLog([NSString stringWithFormat:#"%i", a]);
if(VideoDescription.length < 310)
{
VideoDescription = [VideoDescription stringByAppendingString:#"\n_______________________________________________"];
VideoDescription = [VideoDescription stringByAppendingString:#"\n\nFor the best viewing experience, please use WiFi."];
VideoDescription = [VideoDescription stringByAppendingString:#"\n\nWant an app like this for your YouTube Channel? Go to www.apps4tubers.com to find out how you can get one!"];
VideoDescription = [VideoDescription stringByAppendingString:#"\n\nDon't forget to rate this app! Love it? Hate it? Regardless we love to hear your comments and suggestions!"];
}
else if(VideoDescription.length < 380)
{
VideoDescription = [VideoDescription stringByAppendingString:#"\n_______________________________________________"];
VideoDescription = [VideoDescription stringByAppendingString:#"\n\nFor the best viewing experience, please use WiFi."];
VideoDescription = [VideoDescription stringByAppendingString:#"\n\nWant an app like this for your YouTube Channel? Go to www.apps4tubers.com to find out how you can get one!"];
}
else if(VideoDescription.length > 379 && VideoDescription.length < 410)
{
VideoDescription = [VideoDescription stringByAppendingString:#"\n_______________________________________________"];
VideoDescription = [VideoDescription stringByAppendingString:#"\n\nWant an app like this for your YouTube Channel? Go to www.apps4tubers.com to find out how you can get one!"];
}
else
{
VideoDescription = [VideoDescription stringByAppendingString:#"\n\nFor the best viewing experience, please use WiFi."];
}
CGRect scrollViewFrame = CGRectMake(0, 0, 320, 480);
TheView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
CGSize scrollViewContentSize = CGSizeMake(320, 650);
[TheView setContentSize:scrollViewContentSize];
[TheView setPagingEnabled:NO];
TheView.showsHorizontalScrollIndicator = NO;
TheView.showsVerticalScrollIndicator = YES;
TheView.bounces = NO;
[self.view addSubview:TheView];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
Background = [[UILabel alloc] initWithFrame: CGRectMake(00, 25, 1000, 1600)];
Background.center = CGPointMake(00, 00);
Background.backgroundColor = [self colorWithHexString:#"111625"];
[TheView addSubview: Background];
VideoDecrip = [[UILabel alloc] initWithFrame: CGRectMake(00, 25, 320, 400)];
VideoDecrip.center = CGPointMake(160.5, 400);
VideoDecrip.text = VideoDescription;
VideoDecrip.font = [UIFont fontWithName: #"Cochin-Bold" size: 13.5];
VideoDecrip.textColor = [self colorWithHexString:#"D1E751"];
VideoDecrip.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.35];
VideoDecrip.layer.shadowOpacity = 0;
VideoDecrip.shadowOffset = CGSizeMake(1,1);
VideoDecrip.layer.masksToBounds = NO;
VideoDecrip.backgroundColor = [UIColor clearColor];
VideoDecrip.textAlignment = NSLineBreakByWordWrapping;
VideoDecrip.adjustsFontSizeToFitWidth = YES;
VideoDecrip.numberOfLines = 0;
[VideoDecrip sizeToFit];
[TheView addSubview: VideoDecrip];
NSLog(#"%i", VideoDecrip.text.length);
VideoTitleBorder = [[UILabel alloc] initWithFrame: CGRectMake(00, 25, 320.5, 30)];
VideoTitleBorder.center = CGPointMake(160, 182.5);
VideoTitleBorder.shadowOffset = CGSizeMake(1,1);
VideoTitleBorder.backgroundColor = [self colorWithHexString:#"0B486B"];
VideoTitleBorder.layer.borderColor = [UIColor darkGrayColor].CGColor;
VideoTitleBorder.layer.borderWidth = .4;
[TheView addSubview: VideoTitleBorder];
VideoTitle = [[UILabel alloc] initWithFrame: CGRectMake(00, 25, 320, 26)];
VideoTitle.text = VidTitle;
if([VideoTitle.text length] < 20)
{
VideoTitle.center = CGPointMake(160, 182.5);
}
else if([VideoTitle.text length] < 30)
{
VideoTitle.center = CGPointMake(160, 182);
}
else
{
VideoTitle.center = CGPointMake(160, 179);
}
VideoTitle.font = [UIFont fontWithName: #"ArialRoundedMTBold" size: 25];
VideoTitle.textColor = [self colorWithHexString:#"BEF202"];
VideoTitle.shadowColor = [UIColor blackColor];
VideoTitle.backgroundColor = [UIColor clearColor];
VideoTitle.shadowOffset = CGSizeMake(1,1);
VideoTitle.textAlignment = NSTextAlignmentCenter;
VideoTitle.adjustsFontSizeToFitWidth = YES;
[TheView addSubview: VideoTitle];
VideoTitleBorder2 = [[UILabel alloc] initWithFrame: CGRectMake(00, 25, 160, 500)];
VideoTitleBorder2.center = CGPointMake(400, 125);
VideoTitleBorder2.shadowOffset = CGSizeMake(1,1);
VideoTitleBorder2.backgroundColor = [self colorWithHexString:#"0B486B"];
VideoTitleBorder2.layer.borderColor = [UIColor darkGrayColor].CGColor;
VideoTitleBorder2.layer.borderWidth = 2.0;
[self.view addSubview: VideoTitleBorder2];
In short, you can use the NSString method -sizeWithFont:forWidth:lineBreakMode: to determine how tall the string will render in a container with the given width and line break mode. You can use that to size your UILabel and to set the scrollview's contentsize appropriately.
Try using sizeToFiton the label, then setting the scrollView's contentSize:
[label sizeToFit];
[scrollView setContentSize:CGSizeMake(CGRectGetWidth(self.view.frame), CGRectGetHeight(label.frame) + 10)];
Using sizeToFit is really nice, just make sure your label is already the right width before you call it.

How can I know which view I am clicking? And why the button no reaction?

I am designing a viewcontroller which have several button bar, each bar canbe clicked and show a content view.like:
http://i.stack.imgur.com/m5V4Q.png
When I click the buttonbar, it's frame will expand bigger, and you can see the content in it(which is a button).
First, a bar button(320*30 size) and a contentView is a set, they are subviews of listview. And several listview makes the whole view.
Second, when I click it, the list view will expand from 320*30 to 320*180, the contentview in it will expand from 300*0 to 300 * 130(20 pixel of margin). and I set the clipsToBounds of contentview to Yes to make sure the content in contentview won't show when the frame height is 0.
Now,the click can show the contentview exactly as I want. But here is a problem: I can't click the button in it, and I tried to set their userInteractionEnabled to yes . And even I set the contentview as user-defined,which I set the view's userInteractionEnabled to yes and overwrite the touchbegin function to show a alertView. Those test are failed, no reaction found. And I checked and sure that there shouldn't be any view covered it.
What's the reason might be?
The code of setup the list view is:
NSArray *array = [titleArray objectAtIndex:i];
NSString *ShenSuoTitle = [array objectAtIndex:0];
NSString *ShenSuoContent = [array objectAtIndex:1];
UIView *listView = [[UIView alloc] initWithFrame:CGRectMake(0, totalHeight, 320, ShenSuoViewHeight)];
[listView setBackgroundColor:[UIColor blackColor]];
[listView setTag:[[NSString stringWithFormat:#"%d",(i+1)] intValue]];
[self.scrollView addSubview:listView];
totalHeight = totalHeight + 1 + ShenSuoViewHeight;
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, ShenSuoViewHeight)];
[titleView setBackgroundColor:[UIColor whiteColor]];
[titleView setTag:[[NSString stringWithFormat:#"%d1",i+1] intValue]];
[listView addSubview:titleView];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTag:[[NSString stringWithFormat:#"%d2",i+1] intValue]];
[btn setFrame:CGRectMake(0, 0, 320, ShenSuoViewHeight)];
[btn addTarget:self action:#selector(reSetFrame:) forControlEvents:UIControlEventTouchUpInside];
[titleView addSubview:btn];
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(5, 12, 30, 30)];
img.image = [UIImage imageNamed:#"list_ico.png"];
[img setTag:[[NSString stringWithFormat:#"%d3",i+1] intValue]];
[titleView addSubview:img];
NSLog(#"img:%f,%f",img.frame.origin.y,img.frame.size.height);
UILabel *labTitle = [[UILabel alloc] initWithFrame:CGRectMake(35, 15, 100, 25)];
labTitle.textColor = [UIColor blackColor];
labTitle.backgroundColor = [UIColor clearColor];
labTitle.font = [UIFont boldSystemFontOfSize:15];
labTitle.text = ShenSuoTitle;
[titleView addSubview:labTitle];
NSLog(#"labTitle:%f,%f",labTitle.frame.origin.y,labTitle.frame.size.height);
//add a label for selected
UILabel *selectedLabel = [[UILabel alloc] initWithFrame:CGRectMake(214, 14, 86, 21)];
selectedLabel.textColor = [UIColor grayColor];
//selectedLabel.alpha = 0.75;
selectedLabel.backgroundColor = [UIColor clearColor];
selectedLabel.text = #"All";
selectedLabel.font = [UIFont boldSystemFontOfSize:15];
[selectedLabel setTag:[[NSString stringWithFormat:#"%d5",i+1] intValue]];
[titleView addSubview:selectedLabel];
NSLog(#"selectedLabel:%f,%f",selectedLabel.frame.origin.y,selectedLabel.frame.size.height);
UIView *content = [[UIView alloc] initWithFrame:CGRectMake(10, 10 + ShenSuoViewHeight, 300, 0)];
content.backgroundColor = [UIColor grayColor];
UILabel *testLa = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
testLa.text = #"Label";
UIButton *testBut = [UIButton buttonWithType:UIButtonTypeRoundedRect];
testBut.frame = CGRectMake(0, 40, 100, 30);
testBut.titleLabel.textColor = [UIColor greenColor];
testBut.titleLabel.text = #"Button";
content.tag = [[NSString stringWithFormat:#"%d4",i+1] intValue];
[content addSubview:testLa];
[content addSubview:testBut];
content.clipsToBounds = YES;
[testLa release];
[listView addSubview:content];
[content release];
[labTitle release];
[img release];
[titleView release];
[listView release];
the code handle the click to make the list view expands is:
UIView *titleView = (UIView *)[self.view viewWithTag:[[NSString stringWithFormat:#"%d1",i+1] intValue]];
titleView.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:0.9];
UIView *listView = (UIView *)[self.view viewWithTag:[[NSString stringWithFormat:#"%d",i+1] intValue]];
listView.frame = CGRectMake(0, totalHeight, 320, ShenSuoViewHeight);
totalHeight = totalHeight + 1 + 20 + 180 + ShenSuoViewHeight;
UIView *content = [self.view viewWithTag:[[NSString stringWithFormat:#"%d4",i+1] intValue]];
content.frame = CGRectMake(10, 10 + ShenSuoViewHeight, 300, 180);
UIImageView *img = (UIImageView*)[self.view viewWithTag:[[NSString stringWithFormat:#"%d3",i+1] intValue]];
img.image = [UIImage imageNamed:#"list_ico_d.png"];
I'm not sure I follow your situation exactly, and some code would be nice. To answer your question though, the reason is most likely that the touch is being intercepted elsewhere. It may also be that you just forgot to connect an action method to the button. Try using breakpoints or NSLogs to see what methods are being triggered and which aren't.
Thanks a lot. It is due to my fault. The code of expanding the bar which setting the listview's frame is wrong, it should add in the expanded height. This leads the visual is ok but the clicking not be handled.
So this problem closed.

Resources