Custom UIView subClass with UIScrollView - ios

I am creating a class, that creates an option bar with multiple scroll views accessible by buttons.
This is how I add the subview with its content:
NSArray *scroll1 = [NSArray arrayWithObjects:#"normal",#"tiltshift",#"zoom",#"normal",#"blur",#"outline",#"coming soon", nil];
NSArray *scroll2 = [NSArray arrayWithObjects:#"Emoji Natur-01",#"Emoji Natur-02",#"Emoji Natur-03",#"Emoji Natur-04",#"Emoji Natur-05",#"Emoji Natur-06",#"Emoji Natur-07",#"Emoji Natur-08",#"Emoji Natur-09",#"Emoji Natur-10",#"Emoji Natur-11",#"Emoji Natur-12", nil];
NSArray *scroll3 = [NSArray arrayWithObjects:#"Linear",#"Parallel",#"Parallel 2",#"Crescendo",#"Dubstep",#"Blackhole",#"coming soon", nil];
NSArray *content = [NSArray arrayWithObjects:scroll1,scroll2,scroll3, nil];
[self.view addSubview:[self.bottomOptionView initWithFrame:self.view.frame withContent:content]];
The Subclass then generate a scrollview for each Array, with the buttons based on the content of the array:
-(void)addScrollViewOfContent:(NSArray*)array{
int viewNumber = array.count;
for (int i = 0 ; i < viewNumber; i++) {
//create the main buttons
NSArray *content = array[i];
UIButton *buttonAccess = [UIButton buttonWithType:UIButtonTypeRoundedRect];
CGFloat buttonEdge = 40;
CGFloat buttonSpace = self.frame.size.width /viewNumber;
int placeOfButton = i+1;
CGFloat buttonAnchor = ((placeOfButton*(buttonSpace)) - (buttonSpace /2+ buttonEdge/2));
buttonAccess.frame = CGRectMake(buttonAnchor, 0 , buttonEdge, buttonEdge);
[buttonAccess setBackgroundColor:[UIColor redColor]];
buttonAccess.tag = i;
[buttonAccess addTarget:self action:#selector(buttonAccess:) forControlEvents:UIControlEventTouchDown];
[self addSubview:buttonAccess];
UIScrollView *ScrollView = [[UIScrollView alloc]init];
ScrollView.frame = CGRectMake(0, 50, self.frame.size.width, self.frame.size.height);
ScrollView.showsHorizontalScrollIndicator = YES;
ScrollView.tag = i;
ScrollView.backgroundColor =[UIColor colorWithRed:0 green:0.0 blue:0.0 alpha:0.0];
ScrollView.indicatorStyle = UIScrollViewDecelerationRateNormal ;
//UIImageView *selector = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"FILTER_SELECT.png"]];
CGFloat btnX = 0;
for (int i = 0 ; i < content.count; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(btnX, 2 , 65, 65);
UIImage *img = [UIImage imageNamed:[content objectAtIndex:i]];
[button setBackgroundImage:img forState:UIControlStateNormal];
[button addTarget:self action:#selector(subButtonTarget:) forControlEvents:UIControlEventTouchDown];
button.tag = i;
[button setTitle:[content objectAtIndex:i] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:#"MyriadPro-Cond" size:15];
button.titleLabel.textColor = [UIColor whiteColor];
button.titleLabel.textAlignment = UIBaselineAdjustmentAlignBaselines ;
button.showsTouchWhenHighlighted = YES;
[ScrollView addSubview:button];
btnX = btnX + 100;
}
btnX = btnX - 80;
ScrollView.contentSize = CGSizeMake(btnX + 50, 80);
ScrollView.hidden = YES;
[self.scrollViewArray addObject:ScrollView];
[self addSubview:ScrollView];
}
This gives me exactly what I want.
Then I detect which button, of which scroll view is pressed to trigger the action.
-(void)subButtonTarget:(UIButton *)sender{
int button = sender.tag;
int scrollview = self.selectedScrollView;
[self.videoEditor subAction:button ofScrollView:scrollview];
}
-(void)buttonAccess:(UIButton *)sender{
// self.selectedScrollView = sender.tag;
for (int i=0; i< self.scrollViewArray.count; i++) {
UIScrollView *scrollview= [self.scrollViewArray objectAtIndex:i];
scrollview.hidden= YES;
}
int scrollIndex = sender.tag;
UIScrollView *scrollview= [self.scrollViewArray objectAtIndex:scrollIndex];
scrollview.hidden = NO;
}
Basically when one of the button is touched, it calls a function in the view controller in which the sublclass was initially called.
The problem is that, on touchevent nothing happens, except for NSLog. Would that be because of the dependency relation of the subClass and the VC that uses it?

You should set this one for each UIScrollView
ScrollView.delaysContentTouches = NO;
And try to subclass UIScrollView with override method
-(BOOL)touchesShouldCancelInContentView:(UIView *)view
{
return YES;
}

Related

UIView coordinate conversion has an exception. convertPoint: toView: not work well

I try to make a navigation bar indicator view animation. The layout has some problem.
Expects this:
My solution is to convert the position in the navigation bar's bottom position to self.navigationItem.titleView.
The navigation bar indicator view should belong to the view controller. Not the navigation controller. So indicator view is on self.navigationItem.titleView.
While its position is the navigation bar's bottom. Y position is Point (0, 64) exactly.
So a method convertPoint: toView: is what I need .
Here is code:
- (void)setupTitlesView
{
UIView *titlesView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 100, 35)];
self.navigationItem.titleView = titlesView;
CGPoint new_point = [self.view convertPoint: CGPointMake(0, 64) toView: self.navigationItem.titleView];
NSLog(#"\n\n%#\n\n", NSStringFromCGPoint(new_point));
UIView *indicatorView = [[UIView alloc] initWithFrame: CGRectMake(0, new_point.y - 2, 0, 2)];
indicatorView.tag = -1;
self.indicatorView = indicatorView;
NSArray *titles = #[#"商品", #"详情"];
CGFloat width = titlesView.frame.size.width / titles.count;
CGFloat height = titlesView.frame.size.height;
for (NSInteger i = 0; i<titles.count; i++) {
UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake(i*width, 0, width, height)];
button.tag = i;
[button setTitle:titles[i] forState:UIControlStateNormal];
// color , font ...
[titlesView addSubview:button];
// default click
if (i == 0) {
button.enabled = NO;
self.selectedButton = button;
[button.titleLabel sizeToFit];
CGRect tempFrame = self.indicatorView.frame;
tempFrame.size.width = button.titleLabel.frame.size.width;
self.indicatorView.frame = tempFrame;
CGPoint tempCenter = self.indicatorView.center;
tempCenter.x = button.center.x;
self.indicatorView.center = tempCenter;
}
}
[titlesView addSubview: indicatorView];
}
The result prints :
{0, 64}
Apple says:
convertPoint:toView: Converts a point from the receiver’s coordinate
system to that of the specified view.
How to fix it?
If I were you,I would try:
- (void)setupTitlesView
{
UIView *titlesView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 100, 35)];
self.navigationItem.titleView = titlesView;
UIView *indicatorView = [[UIView alloc] initWithFrame: CGRectMake(0, titlesView.frame.size.height - 2, 50, 2)];
indicatorView.tag = -1;
self.indicatorView = indicatorView;
self.indicatorView.backgroundColor = [UIColor blueColor];
NSArray *titles = #[#"商品", #"详情"];
CGFloat width = titlesView.frame.size.width / titles.count;
CGFloat height = titlesView.frame.size.height;
for (NSInteger i = 0; i<titles.count; i++) {
UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake(i*width, 0, width, height)];
button.tag = i+1000;
[button setTitle:titles[i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
[titlesView addSubview:button];
// default click
if (i == 0) {
button.selected = YES;
}
}
[titlesView addSubview: indicatorView];
}
- (void)buttonClicked:(UIButton *)sender
{
for (int i = 0; i < 2; i++) {
UIButton *button = [self.navigationItem.titleView viewWithTag:(1000+i)];
button.selected = (button.tag == sender.tag);
}
[UIView animateWithDuration:0.3 animations:^{
self.indicatorView.center = CGPointMake(sender.center.x, self.indicatorView.center.y);
}];
}

How to open and close tableView on a button click?

2I'm working on dropDowns, i'm creating buttons and dropdown tableView dynamically. When i click first button last tableView was opened. But i want to open specific tableView (at a time i want to open only one dropdown).
float y = 0;
float x = (self.answersView.frame.size.width/2)+175;
float Iy = 0;
float Ix = 675;
float Tx = (self.answersView.frame.size.width/2)+175;
float Ty = 0;
for (int i=0; i<self.radioBtnTagArr.count; i++) {
self.dropdownBtn = [UIButton buttonWithType:UIButtonTypeSystem];
self.dropdownBtn.frame = CGRectMake(x, y+30, (self.answersView.frame.size.width/2)-200, 50);
[self.dropdownBtn setTitle:#"0" forState:UIControlStateNormal];
self.dropdownBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[self.dropdownBtn setTitleColor:[UIColor colorWithRed:24/255.0 green:56/255.0 blue:131/255.0 alpha:1] forState:UIControlStateNormal];
self.dropdownBtn.titleLabel.font = [UIFont systemFontOfSize:25];
[self.dropdownBtn addTarget:self action:#selector(dropdownBtnTapMethod:) forControlEvents:UIControlEventTouchUpInside];
self.dropdownBtn.backgroundColor = [UIColor lightGrayColor];
[self.scrollView addSubview:self.dropdownBtn];
self.dropdownBtn.tag = [[self.btnTagArr objectAtIndex:i] intValue];
y = y+70;
self.dropdownImageView = [[UIImageView alloc]initWithFrame:CGRectMake(Ix, Iy+50, 20, 10)];
self.dropdownImageView.image = [UIImage imageNamed:#"dropdown.png"];
[self.answersView addSubview:self.dropdownImageView];
Iy = Iy+70;
self.dropdownTableView = [[UITableView alloc]initWithFrame:CGRectMake(Tx, Ty+80, (self.answersView.frame.size.width/2)-200, 50) style:UITableViewStylePlain];
self.content = #[ #"Monday", #"Tuesday", #"Wednesday",#"Thursday",#"Friday",#"Saturday",#"Sunday"];
self.dropdownTableView.delegate = self;
self.dropdownTableView.dataSource = self;
[self.answersView addSubview:self.dropdownTableView];
Ty = Ty+70;
self.dropdownTableView.hidden = YES;
}
//The event handling method
- (void)dropdownBtnTapMethod:(UIButton *) sender {
if (self.dropdownTableView.hidden == YES) {
self.dropdownTableView.hidden = NO;
} else {
self.dropdownTableView.hidden = YES;
}
}
And how to set table view height dynamically.
Try this :
NSMutableArray *arrTableview; // make global
In ViewDidLoad
arrTableview = [NSMutableArray New];
Change in following method
for (int i=0; i<self.radioBtnTagArr.count; i++) {
self.dropdownBtn = [UIButton buttonWithType:UIButtonTypeSystem];
self.dropdownBtn.tag = i; // add this line
self.dropdownBtn.frame = CGRectMake(x, y+30, (self.answersView.frame.size.width/2)-200, 50);
[self.dropdownBtn setTitle:#"0" forState:UIControlStateNormal];
self.dropdownBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[self.dropdownBtn setTitleColor:[UIColor colorWithRed:24/255.0 green:56/255.0 blue:131/255.0 alpha:1] forState:UIControlStateNormal];
self.dropdownBtn.titleLabel.font = [UIFont systemFontOfSize:25];
[self.dropdownBtn addTarget:self action:#selector(dropdownBtnTapMethod:) forControlEvents:UIControlEventTouchUpInside];
self.dropdownBtn.backgroundColor = [UIColor lightGrayColor];
[self.scrollView addSubview:self.dropdownBtn];
//self.dropdownBtn.tag = [[self.btnTagArr objectAtIndex:i] intValue]; // This line commented.
y = y+70;
self.dropdownImageView = [[UIImageView alloc]initWithFrame:CGRectMake(Ix, Iy+50, 20, 10)];
self.dropdownImageView.image = [UIImage imageNamed:#"dropdown.png"];
[self.answersView addSubview:self.dropdownImageView];
Iy = Iy+70;
self.dropdownTableView = [[UITableView alloc]initWithFrame:CGRectMake(Tx, Ty+80, (self.answersView.frame.size.width/2)-200, 50) style:UITableViewStylePlain];
self.content = #[ #"Monday", #"Tuesday", #"Wednesday",#"Thursday",#"Friday",#"Saturday",#"Sunday"];
self.dropdownTableView.delegate = self;
self.dropdownTableView.dataSource = self;
[self.answersView addSubview:self.dropdownTableView];
[arrTableview addObject :self.dropdownTableView];
Ty = Ty+70;
self.dropdownTableView.hidden = YES;
}
Selector
- (void)dropdownBtnTapMethod:(UIButton *) sender {
UITableView *tableView =[arrTableview objectAtIndex:sender.tag]
if (tableView.hidden == YES) {
tableView.hidden = NO;
} else {
tableView.hidden = YES;
}
}

Horizontal UIScrollView swipe left and right doesn't work after long gesture?

I have a scrollview. It behaves as a slider.(Left to right, Right to left). If I swipe left or swipe right, it works. No problem. But If firstly I make long press and then I try to swipe left or right, it doesn't work.
- (void)initCarouselScroll
{
[self.carouselScroll removeFromSuperview];
self.carouselScroll = nil;
self.carouselScrollHeight = self.view.frame.size.height;
self.carouselScrollWidth = self.view.frame.size.width;
self.carouselScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.carouselScrollWidth, self.carouselScrollHeight)];
self.carouselScroll.backgroundColor = [UIColor clearColor];
self.carouselScroll.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.carouselScroll.tag = 500;
self.carouselScroll.showsHorizontalScrollIndicator = NO;
self.carouselScroll.directionalLockEnabled = YES;
self.carouselScroll.showsVerticalScrollIndicator = NO;
self.carouselScroll.pagingEnabled = YES;
self.carouselScroll.delegate = self;
self.carouselScroll.contentSize = CGSizeMake(self.carouselScrollWidth * [self.bannerList count], self.carouselScrollHeight);
[self.carouselScroll setContentOffset:CGPointZero];
[self.view addSubview:self.carouselScroll];
[self.view sendSubviewToBack:self.carouselScroll];
NSUInteger maxLoad = 3;
if ([self.bannerList count] < maxLoad) {
maxLoad = [self.bannerList count];
}
for (int i = 0; i < maxLoad; i++) {
[self loadImageFor:i];
}
[self updateSelectedDotImage:0];
}
- (void)loadImageFor:(int)index
{
if (index < 0 || index >= [self.bannerList count]) {
return;
}
AnyImageView* imageView = (AnyImageView*)[self.carouselScroll viewWithTag:index];
if (!imageView) {
Banner* banner = [self.bannerList objectAtIndex:(index)];
CGRect frame = CGRectMake(self.carouselScrollWidth * (index), 0, self.carouselScrollWidth, self.carouselScrollHeight);
NSString* imageUrl = banner.iphoneImage;
if (self.deviceType == DeviceTypeIpad) {
imageUrl = banner.ipadImage;
}
AnyImageView* imageView = [[AnyImageView alloc] initWithFrame:frame picPath:imageUrl isWhite:NO];
imageView.tag = index;
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(bannerButtonClicked:)];
UILongPressGestureRecognizer* longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleLongPressGestures:)];
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addGestureRecognizer:singleTap];
[button removeGestureRecognizer:longTap];
button.frame = frame;
button.tag = index;
button.backgroundColor = [UIColor clearColor];
[self.carouselScroll addSubview:imageView];
[self.carouselScroll addSubview:button];
}
}

Get selected button value from the UIButton

From the array I am creating button with the array values inside each button, Here I want to place a radio button before each button. while clicking the radio button i want to get it selected or not. In the below code I can display the button and the radio button image but cannot get the selected button value. How can i get the radio button selected or not for each button
//My Array
labelArrays = [NSMutableArray arrayWithObjects:#"one", #"two", #"three",#"four",#"five",#"six", nil];
//Creating button with placing a radio button image inside
-(void) createbutton:(CGRect) frame {
CGFloat xCoordinate = frame.origin.x;
CGFloat yCoordinate = frame.origin.y;
CGFloat buttonHeight = frame.size.height;
CGFloat buttonWidth = frame.size.width;
CGFloat gapBetweenTwoButton = 2;
for(int counter = 0; counter < [labelArrays count]; counter++) {
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button =[UIColor clearcolor];
[button setFrame:CGRectMake(xCoordinate,yCoordinate, 100, 40)];
NSString* titre = [labelArrays objectAtIndex:counter];
[button setImage:[UIImage imageNamed:#"radio_selected.png"] forState:UIControlStateNormal];
button.titleEdgeInsets = UIEdgeInsetsMake(0,10,0,0);
[button setTitle:[NSString stringWithFormat:#"%#",titre] forState:UIControlStateNormal];
[button setUserInteractionEnabled:YES];
[self. button addTarget: self action: #selector(buttonAction:)forControlEvents: UIControlEventTouchUpInside];
[self addSubview: button];
if((counter+1)%3 == 0) {
xCoordinate = 0;
yCoordinate = yCoordinate + buttonHeight + gapBetweenTwobutton;
xCoordinate = xCoordinate + buttonWidth + gapBetweenTwoLabels;
}
}
//my frame
[self createbutton:CGRectMake(32,20,220,40)];
It's very simple to get selected button value by using its tag value. See here and try below code.
Put these line in viewDidLoad()
labelArrays = [NSMutableArray arrayWithObjects:#"one", #"two", #"three", #"four", #"five", #"six", nil];
[self createButtons];
And then define these methods.
- (void)createButtons
{
int x = 30, y = 20;
for (int i = 0; i<labelArrays.count; i++)
{
UIButton *btnImage = [UIButton buttonWithType:UIButtonTypeCustom];
btnImage.frame = CGRectMake(x, y, 100, 40);
btnImage.backgroundColor = [UIColor redColor];
btnImage.tag = 700+i;
[btnImage setImage:[UIImage imageNamed:#"radio_unselect.png"] forState:UIControlStateNormal];
btnImage.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
[btnImage setTitle:[NSString stringWithFormat:#"%#", [labelArrays objectAtIndex:i]] forState:UIControlStateNormal];
[btnImage addTarget: self action: #selector(changeValue:)forControlEvents: UIControlEventTouchUpInside];
[self.view addSubview:btnImage];
if ((i+1)%2 == 0)
{
x = 30;
y = y+btnImage.frame.size.height+10;
}
else
{
x = x+btnImage.frame.size.width+30;
}
}
}
- (IBAction)changeValue:(UIButton *)sender
{
UIButton *btn = (UIButton *)[self.view viewWithTag:sender.tag];
if (btn.selected == TRUE)
{
[btn setImage:[UIImage imageNamed:#"radio_unselect.png"] forState:UIControlStateNormal];
btn.selected = FALSE;
}
else
{
[btn setImage:[UIImage imageNamed:#"radio_select.png"] forState:UIControlStateNormal];
btn.selected = TRUE;
NSLog(#"%#", btn.titleLabel.text);
}
}

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.

Resources