Change text on swipe - ios

I am creating a text jokes app what i need is i want the UITextView in swipe form like when i swipe the first joke after a gap another UITextview comes in with another joke , so far what i have done is i have manually created the UITextView ( like if there is 10 jokes then 10 uitextview ) and added them in a scroll but that is taking too much time in viewdidload method and the app takes 6,7 second to load ...
if you have anyother idea please do let me know
for (int i = 0; i < arrresult.count; i++) {
CGRect frame;
frame.origin.x = self.scrollmain.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollmain.frame.size;
// UITextView *subview = [[UITextView alloc] initWithFrame:CGRectMake((frame.origin.x)+40,(frame.origin.y)+10, 230, 120)];
// UITextView *subview2 = [[UITextView alloc]initWithFrame:CGRectMake((frame.origin.x)+40,(frame.origin.y)+160,230, 120)];
UITextView *subview = [[UITextView alloc] initWithFrame:CGRectMake((frame.origin.x)+40,(frame.origin.y)+10, 230, 120)];
UITextView *subview2 = [[UITextView alloc]initWithFrame:CGRectMake((frame.origin.x)+40,(frame.origin.y)+160,230, 120)];
subview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"BG_text.png"]];
subview2.backgroundColor = [UIColor whiteColor];
[subview setFont:[UIFont boldSystemFontOfSize:14]];
[subview2 setFont:[UIFont boldSystemFontOfSize:14]];
subview.layer.cornerRadius = 10 ;
subview2.layer.cornerRadius = 10;
NSString *p = [duaobj2 dua_arabic];
NSString *b = [duaobj2 eng_trans];
subview.text = p;
subview2.text = b;
[self.scrollmain addSubview:subview];
[self.scrollmain addSubview:subview2];
subview.editable = NO;
subview2.editable = NO;
subview.textColor = [UIColor blackColor];
subview2.textColor = [UIColor blackColor];
subview.backgroundColor = [UIColor whiteColor];
subview2.backgroundColor = [UIColor whiteColor];
scrollmain.showsHorizontalScrollIndicator = NO;
CGRect frame1 = subview.frame;
CGRect frame2 = subview2.frame;
frame1.size.height = subview.contentSize.height;
frame2.size.height = subview2.contentSize.height;
subview.frame = frame1;
subview2.frame = frame2;
}
self.scrollmain.contentSize = CGSizeMake(self.scrollmain.frame.size.width * arrresult.count,self.scrollmain.frame.size.height);
scrollmain.pagingEnabled = TRUE;
scrollmain.delegate = self;

There is no need to load the subviews at the time of viewDidLoad.
You should delay this by using scrollView delegate methods.
You should create only 3 textviews at max. Current, previous and next.
On scrollViewDidScroll delegate method, you should wisely remove previously loaded textview and you should add new textview.

Related

Incorrect Page size in UIScrollview Paging

I am trying to make a simple Paging with UIScrollview, But pages are getting out after the second screen. Here is my code.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSArray *colors =[NSArray arrayWithObjects:#"1. This is my first Page ",#"2. This is my Second Page",#"3. This is my third Page",#"4. This is my fourth Page",#"5. This is my fifth Page",nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = (scroll_events.frame.size.width) * i;
frame.origin.y = 0;
frame.size = scroll_events.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
[self.scroll_events addSubview:subview];
UITextView *myTextView = [[UITextView alloc] init];
myTextView.text = colors[i];
[subview addSubview:myTextView];
[myTextView setBackgroundColor: [UIColor redColor]];
[myTextView setTextColor: [UIColor blackColor]];
[myTextView setFont: [UIFont fontWithName:#"Lobster 1.4" size:30]];
myTextView.frame = CGRectMake(0.0,0.0,scroll_events.bounds.size.width,scroll_events.bounds.size.height);
myTextView.editable = NO;
myTextView.selectable = NO;
myTextView.scrollEnabled = NO;
}
self.scroll_events.contentSize = CGSizeMake(self.scroll_events.frame.size.width * colors.count, self.scroll_events.frame.size.height);
}
Here is the screen shot of my Scroll View
In ViewDidLoad maybe the frame of your scroll view was still not set.
Try that code in ViewDidAppear and check if it works.
Advice: You should subclass that UISCrollView and initialize the view there, not in the ViewController.
Let me know it that worked!
Regards.

iOS FadeIn/FadeOut during paging

I have an UIScrollView with views inside that I can scroll horizontally. During the scroll I would like to add a fade out on the view which is going out of the screen and a fade in on the view which is going in the screen.
Here my code :
for (int i = 0; i <textEntries.count; i++)
{
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i + 5;
frame.size.width = self.scrollView.frame.size.width - 10;
frame.size.height = self.scrollView.frame.size.height - 10;
self.scrollView.pagingEnabled = YES;
UIView *subview = [[UIView alloc] initWithFrame:frame];
if([[colorEntries objectAtIndex:i] isEqualToString:#"violet"]){
subview.backgroundColor = [UIColor colorWithRed:(147/255.0) green:(90/255.0) blue:(255/255.0) alpha:1];
} else {
subview.backgroundColor = [UIColor colorWithRed:(100/255.0) green:(166/255.0) blue:(255/255.0) alpha:1];
}
subview.layer.cornerRadius = 5;
[self.scrollView addSubview:subview];
UITextView *noteLabel = [[UITextView alloc] init];
noteLabel.frame = CGRectMake(0, 0, self.scrollView.frame.size.width - 20, self.scrollView.frame.size.height -20);
noteLabel.text = [textEntries objectAtIndex:i];
noteLabel.userInteractionEnabled = NO;
noteLabel.backgroundColor = [UIColor clearColor];
noteLabel.contentInset = UIEdgeInsetsMake(20,20,20,20);
noteLabel.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:40];
[noteLabel setTextColor:[UIColor whiteColor]];
[subview addSubview:noteLabel];
[noteLabel addObserver:self forKeyPath:#"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
}
Could you help me to build this fadeIn/FadeOut effect ?
Thanks!

Not able to scroll images in iOS

I have create scrollview inside that i am displaying tiles using this i can swap images.if i take 9 images it displaying in scrollview but If i give setUserInteractionEnabled:NO means i can tiles i can swap but If i give setUserInteractionEnabled:YES then its scroll is working not tiles i can not swap image and not even touch images.Can any one tell me what is the mistake in this below code..
-(void)ViewDidLoad
{
thumbnailscrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
thumbnailscrollview.delegate = self;
thumbnailscrollview.backgroundColor = [UIColor whiteColor];
[thumbnailscrollview setUserInteractionEnabled:YES];
[thumbnailscrollview setContentSize:CGSizeMake(320.0,1600.0)];
thumbnailscrollview.showsVerticalScrollIndicator = YES;
[self.view addSubview:thumbnailscrollview];
[self createTiles];
}
- (void)createTiles {
theviewlabel=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
// [self.view addSubview:thumbnailscrollview];
[thumbnailscrollview addSubview:theviewlabel];
tileindex=0;
UIColor *tileColors[] = {
[UIColor whiteColor],
[UIColor whiteColor],
[UIColor whiteColor],
[UIColor whiteColor],
[UIColor whiteColor],
[UIColor whiteColor],
[UIColor whiteColor],
};
int tileColorCount = sizeof(tileColors) / sizeof(tileColors[0]);
int rows=([_imageCollection1 count]/3)+1;
CGRect frame;
int count=0;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < TILE_COLUMNS; col++) {
if ([_imageCollection1 count] <= count) {
return;
}
int index = (row * TILE_COLUMNS) + col;
frame = CGRectMake(TILE_MARGIN + col * (TILE_MARGIN + TILE_WIDTH),
TILE_MARGIN + row * (TILE_MARGIN + TILE_HEIGHT),
TILE_WIDTH, TILE_HEIGHT);
CGRect frame2 = CGRectMake(TILE_MARGIN + col * (TILE_MARGIN + TILE_WIDTH)-12,
TILE_MARGIN + row * (TILE_MARGIN + TILE_HEIGHT)-3,
12, 12);
tileFrame[index] = frame;
Tile *tile = [[Tile alloc] init];
tile.tileIndex = index;
tile.tag=[[_buttontagcollections1 objectAtIndex:index]intValue];
tile.mRearrangeView=[_imageCollection1 objectAtIndex:count];
count++;
tileForFrame[index] = tile;
tile.frame = frame;
tile.backgroundColor = tileColors[index % tileColorCount].CGColor;
//tile.cornerRadius = 8;
tile.delegate = self;
thelabel=[[UILabel alloc]initWithFrame:frame2];
thelabel.backgroundColor=[UIColor whiteColor];
thelabel.textColor=[UIColor redColor];
thelabel.layer.cornerRadius=7;
//button.titleLabel.textAlignment = NSTextAlignmentCenter;
thelabel.textAlignment=NSTextAlignmentCenter;
thelabel.text=[NSString stringWithFormat:#"%d",tileindex
];
thelabel.font=[UIFont systemFontOfSize:13];
//[self.view.layer addSublayer:tile];
[theviewlabel addSubview:thelabel];
homeView = [[UIView alloc]initWithFrame:CGRectMake(248, -480, 72, 480)];
homeView.backgroundColor = [UIColor clearColor];
[thumbnailscrollview addSubview:homeView];
[tile setNeedsDisplay];
tileindex++;
homeView = [[UIView alloc]initWithFrame:CGRectMake(248, -480, 72, 480)];
homeView.backgroundColor = [UIColor clearColor];
[thumbnailscrollview addSubview:homeView];
[thumbnailscrollview.layer addSublayer:tile];
}
}
}
enter code herewhy are you adjusting content size BEFORE adding subviews to scroll view.Always add content size after adding all subviews to scroll view.The content size of scroll view must be larger than its superview.
what you do :
in viewDIDLoad:
thumbnailscrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y,
self.view.frame.size.width,
self.viewframe.size.height)];
//[thumbnailscrollview setContentSize:CGSizeMake(320.0,1600.0)]; comment this line
and in createTile method, when you are all done adding your subview then set content size:
CGRect contentRect = CGRectZero;
for (UIView *view in self.thumbnailscrollview.subviews)
{
contentRect = CGRectUnion(contentRect, view.frame);
}
self.thumbnailscrollview.contentSize = contentRect.size;
it should work then.
thanks

iOS Image View turn off selection highlighting

Bit of a strange question, so i've attached a screen recording to help...
Video : https://www.dropbox.com/s/3aaefixsk8eejln/Error.mov (See past the watermark!)
My issue is that in my application when the user is at the "Preview" view and is reviewing their images, each time the image is selected, the image receives a touched overlay, which could be confusing for a user as nothing happens when touched.
I would like to disable this if possible..
Below is the example code to how the images are being displayed..
- (void)setReviewImages
{
continueButtonDisabled = YES;
downloadedImageCount = 0;
NSArray *reviewViews = scrollView.subviews;
for (IMReviewView *reviewView in reviewViews) {
[reviewView removeFromSuperview];
}
NSUInteger photoCount = [[IMLocalUser localUser] cachedPhotoCount];
if ( nPageNum == 0 ){
for (NSUInteger i = 0; i < photoCount; i++) {
CGRect frame;
frame.origin.x = scrollView.frame.size.width * i;
frame.origin.y = 65;
frame.size = CGSizeMake(scrollView.frame.size.width, 327.0f);
IMReviewView *subview = [[IMReviewView alloc] initWithFrame:frame];
subview.delegate = self;
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:i];
[scrollView addSubview:subview];
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * i, scrollView.frame.size.height);
UILabel *headingLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 20, 300, 30)];
[self.view addSubview:headingLabel];
headingLabel.text = #"Time To Preview!";
headingLabel.textColor = [UIColor blackColor];
headingLabel.textAlignment = UITextAlignmentCenter;
headingLabel.textAlignment = NSTextAlignmentCenter;
headingLabel.tag = 10;
headingLabel.backgroundColor = [UIColor clearColor];
headingLabel.font = [UIFont boldSystemFontOfSize:26.0f];
headingLabel.hidden = NO;
headingLabel.highlighted = YES;
headingLabel.highlightedTextColor = [UIColor blackColor];
headingLabel.lineBreakMode = YES;
headingLabel.numberOfLines = 0;
}
It seems to me like IMReviewView has a UIButton subview. If so, try to set its adjustsImageWhenHighlighted property to NO.

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.

Resources