create account

How to use TTRangeSlider to filter price value in iOS development by andrixyz

View this thread on: hive.blogpeakd.comecency.com
· @andrixyz · (edited)
$27.05
How to use TTRangeSlider to filter price value in iOS development
![Screen Shot 2018-01-09 at 10.55.01 AM.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1515470167/i7jcqekqpkvsbvqquor9.png)

In this chance, i want to share how to implement TTRangeSlider in iOS development so that we can filter specific things like price, distance, etc. Developers often need to use slider to filter their app need. It is the helpful component that not only help the developers to make the app but also the user so that the user can filter things what they want specifically. So, this is how to use the TTRangeSlider library in iOS development. In this tutorial, i will show with the price filter. 

<h1>What is slider?</h1>
Slider is a horizontal track with a control called a thumb, which you can slide with your finger to move between a minimum and maximum value. We can also use to set the brightness level or position during playing music. 
![Screen Shot 2018-01-09 at 10.59.24 AM.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1515470378/scys9qb3s0ih7vwtwujt.png)
TTRangeSlider.

<h1>How to include the library in the project?</h1>
The most important things to do before including library in iOS development is we must install the Cocoapods. Cocoapods is a dependency manager that will handle various library for iOS apps. You can visit this link to view official installation https://guides.cocoapods.org/using/getting-started.html or read my previous post https://utopian.io/utopian-io/@andrixyz/how-to-implementing-alert-in-ios-using-ehplainalert and look at <b>Installing Cocoapods</b>.
Once you're ready, close the project and always remember to open with the .xcworkspace, the white one so that you can properly using the Pod.
![Screen Shot 2018-01-09 at 11.17.50 AM.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1515471484/liguwxoncle8wsl5zuvt.png)
Then, go to the Podfile and add this line 
```
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'PriceFilterApp' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  # use_frameworks!

  # Pods for PriceFilterApp
  pod 'EHPlainAlert'
  pod 'Masonry'
  pod 'TTRangeSlider'
  target 'PriceFilterAppTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'PriceFilterAppUITests' do
    inherit! :search_paths
    # Pods for testing
  end

end
```
Then don't forget to run ```pod install``` command to analyze and install the TTRangeSlider. Clean and build the project so everything will refresh to work better :).  Then, let's work on.

<h1>Declare in the .h file</h1>
In objective c ios development, there is always to file. The .h and .m file. The .h file is the header which usually we import and declares variable or library that needs the delegate in the .m. Meanwhile, the .m file is the implementation of the .h file. This is how we declare the TTRangeSlider in our PriceViewController.h file
```
//
//  PriceViewController.h
//  AlertDemoApp
//
//  Created by Andri on 1/8/18.
//  Copyright © 2018 Andri. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <TTRangeSlider.h>
#import <Masonry.h>

@interface PriceViewController : UIViewController <TTRangeSliderDelegate>{
    TTRangeSlider *priceSlider;
    UILabel *minPriceLabel;
    UILabel *maxPriceLabel;
}

@end
```
Look at the ```TTRangeSlider *priceSlider`` code. That is we declare the variable first. Don't forget to notice the ```<TTRangeSliderDelegate>```. It means we are assigning the protocol to the PriceViewController class so we can delegate the method.  Then, we're going to use that in PriceViewController.m file. 

<h1>Initializing the TTRangeSlider<h1>
Go to our PriceViewController.m in this context. And write down this code.
```
priceSlider = [TTRangeSlider new];
//    priceSlider.hideLabels = YES;
    priceSlider.delegate = self;
    priceSlider.lineHeight = 3;
    priceSlider.minValue = 0;
    priceSlider.maxValue = 1000000;
    priceSlider.minDistance = 50000;
    priceSlider.enableStep = YES;
    priceSlider.step = 50000;
    priceSlider.selectedMinimum = 0;
    priceSlider.selectedMaximum = 1000000;
    [self.view addSubview:priceSlider];
    [priceSlider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.top.equalTo(priceLabel.mas_bottom).offset(8);
    }];
```
The ```priceSlider.delegate = self``` line means that we delegate it to the view controller so the view controller can override the method so we can manipulate with the results.
The ```priceSlider.lineHeight = 3;``` line means that we just set the line height of the slider
The ```priceSlider.minValue = 0;``` line describes that the minimum value that the user can slide on the slider or we can say it is the limit of minimum
The ```priceSlider.maxValue = 0;``` line describes that the maximum value that the user can slide on the slider or we can say it is the limit of maximum
The ```priceSlider.minDistance = 50000;``` line describes the minimum distance between minimum and maximum value
The ```priceSlider.enableStep = YES;``` line indicates that we enable the step so user can snap at specific value using step property
The ```priceSlider.step = 50000``` line describes that each step will increase 50000 value
The ```priceSlider.selectedMinimum = 0``` line means the current selected minimum value of the slider ball
The ```priceSlider.selectedMaximum = 1000000``` line means the current selected maximum value of the slider ball
The ```[self.view addSubview:priceSlider];``` line means we add the view to our self.view
And the ```[priceSlider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.top.equalTo(priceLabel.mas_bottom).offset(8);
    }];```
line is just as usual we set constraint to the left and right with offset 18 and the top below the priceLabel with offset 8.
That are all the explanation of the declaration codes. Then, we must implement the delegate method of the slider. Write down this code.
```
#pragma mark - TTRangeSliderDelegate
-(void)rangeSlider:(TTRangeSlider *)sender didChangeSelectedMinimumValue:(float)selectedMinimum andMaximumValue:(float)selectedMaximum{
    NSString *min = [NSString stringWithFormat:@"%.0f",selectedMinimum];
    NSString *max = [NSString stringWithFormat:@"%.0f",selectedMaximum];
    minPriceLabel.text = [NSString stringWithFormat:@"Minimum Price: %@ IDR", min];
    maxPriceLabel.text = [NSString stringWithFormat:@"Maximum Price: %@ IDR", max];
}
```
Notice the ```(void)rangeSlider:(TTRangeSlider *)sender didChangeSelectedMinimumValue:(float)selectedMinimum andMaximumValue:(float)selectedMaximum{``` line.
It means we are override the method so we can modify interactions when the user is dragging the slider. In our above code, it assigns the min and max value to the string value and then show it to the label so user can see what is the specific value they choose. 
You can copy this entire PriceViewController.m file though.
```
//
//  PriceViewController.m
//  PriceDemoApp
//
//  Created by Andri on 1/8/18.
//  Copyright © 2018 Andri. All rights reserved.
//

#import "PriceViewController.h"

@interface PriceViewController ()

@end

@implementation PriceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *priceLabel = [UILabel new];
    priceLabel.text = @"Price Filter between 0 IDR and 1000000 IDR";
    [self.view addSubview:priceLabel];
    [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(48);
    }];
    
    priceSlider = [TTRangeSlider new];
//    priceSlider.hideLabels = YES;
    priceSlider.delegate = self;
    priceSlider.lineHeight = 3;
    priceSlider.minValue = 0;
    priceSlider.maxValue = 1000000;
    priceSlider.minDistance = 50000;
    priceSlider.enableStep = YES;
    priceSlider.step = 50000;
    priceSlider.selectedMinimum = 0;
    priceSlider.selectedMaximum = 1000000;
    [self.view addSubview:priceSlider];
    [priceSlider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.top.equalTo(priceLabel.mas_bottom).offset(8);
    }];
    
    minPriceLabel = [UILabel new];
    minPriceLabel.text = @"Minimum Price:";
    [self.view addSubview:minPriceLabel];
    [minPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(priceSlider.mas_bottom).offset(8);
        make.left.equalTo(priceSlider).offset(4);
    }];

    maxPriceLabel = [UILabel new];
    maxPriceLabel.text = @"Maximum Price:";
    [self.view addSubview:maxPriceLabel];
    [maxPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(minPriceLabel.mas_bottom).offset(8);
        make.left.equalTo(minPriceLabel);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - TTRangeSliderDelegate
-(void)rangeSlider:(TTRangeSlider *)sender didChangeSelectedMinimumValue:(float)selectedMinimum andMaximumValue:(float)selectedMaximum{
    NSString *min = [NSString stringWithFormat:@"%.0f",selectedMinimum];
    NSString *max = [NSString stringWithFormat:@"%.0f",selectedMaximum];
    minPriceLabel.text = [NSString stringWithFormat:@"Minimum Price: %@ IDR", min];
    maxPriceLabel.text = [NSString stringWithFormat:@"Maximum Price: %@ IDR", max];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
```
Okay, that's all i want to share about how to implement TTRangeSlider in iOS development. You can modify or add another label to filter distance. It's important just to understand the concepts and each property and the library. Thank you for reading this guide.




<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@andrixyz/how-to-use-ttrangeslider-to-filter-price-value">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍  , , , , , , , , ,
properties (23)
authorandrixyz
permlinkhow-to-use-ttrangeslider-to-filter-price-value
categoryutopian-io
json_metadata{"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"id":33214457,"name":"TTRangeSlider","full_name":"TomThorpe/TTRangeSlider","html_url":"https://github.com/TomThorpe/TTRangeSlider","fork":false,"owner":{"login":"TomThorpe"}},"pullRequests":[],"platform":"github","type":"tutorials","tags":["utopian-io","slider","objective-c","ios","development"],"users":["andrixyz","interface","end","implementation"],"links":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1515470167/i7jcqekqpkvsbvqquor9.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515470378/scys9qb3s0ih7vwtwujt.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515471484/liguwxoncle8wsl5zuvt.png"],"image":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1515470167/i7jcqekqpkvsbvqquor9.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515470378/scys9qb3s0ih7vwtwujt.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515471484/liguwxoncle8wsl5zuvt.png"],"moderator":{"account":"shreyasgune","reviewed":true,"pending":false,"flagged":false}}
created2018-01-09 04:57:54
last_update2018-01-11 06:57:03
depth0
children4
last_payout2018-01-16 04:57:54
cashout_time1969-12-31 23:59:59
total_payout_value18.898 HBD
curator_payout_value8.150 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length10,484
author_reputation1,144,619,513,316
root_title"How to use TTRangeSlider to filter price value in iOS development"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id28,166,707
net_rshares3,075,041,645,521
author_curate_reward""
vote details (10)
@crokkon · (edited)
.
.
properties (22)
authorcrokkon
permlinkre-andrixyz-how-to-use-ttrangeslider-to-filter-price-value-20180109t094819193z
categoryutopian-io
json_metadata"{"app": ""}"
created2018-01-09 09:48:18
last_update2022-09-19 18:24:45
depth1
children0
last_payout2018-01-16 09:48:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1
author_reputation81,214,366,861,104
root_title"How to use TTRangeSlider to filter price value in iOS development"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id28,212,877
net_rshares0
@shreyasgune ·
Thank you for the contribution. It has been approved.

You can contact us on [Discord](https://discord.gg/UCvqCsx).
**[[utopian-moderator]](https://utopian.io/moderators)**
properties (22)
authorshreyasgune
permlinkre-andrixyz-how-to-use-ttrangeslider-to-filter-price-value-20180111t065650666z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-11 06:57:03
last_update2018-01-11 06:57:03
depth1
children1
last_payout2018-01-18 06:57:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length172
author_reputation4,924,803,411,962
root_title"How to use TTRangeSlider to filter price value in iOS development"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id28,683,748
net_rshares0
@andrixyz ·
You're welcome.
properties (22)
authorandrixyz
permlinkre-shreyasgune-re-andrixyz-how-to-use-ttrangeslider-to-filter-price-value-20180112t022647442z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-12 02:26:48
last_update2018-01-12 02:26:48
depth2
children0
last_payout2018-01-19 02:26:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length15
author_reputation1,144,619,513,316
root_title"How to use TTRangeSlider to filter price value in iOS development"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id28,896,493
net_rshares0
@utopian-io ·
### Hey @andrixyz I am @utopian-io. I have just upvoted you!
#### Achievements
- You have less than 500 followers. Just gave you a gift to help you succeed!
- Seems like you contribute quite often. AMAZING!
#### Suggestions
- Contribute more often to get higher and higher rewards. I wish to see you often!
- Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!
#### Get Noticed!
- Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!
#### Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. <a href="https://discord.gg/zTrEMqB">Participate on Discord</a>. Lets GROW TOGETHER!
- <a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for my Witness With SteemConnect</a>
- <a href="https://v2.steemconnect.com/sign/account-witness-proxy?proxy=utopian-io&approve=1">Proxy vote to Utopian Witness with SteemConnect</a>
- Or vote/proxy on <a href="https://steemit.com/~witnesses">Steemit Witnesses</a>

[![mooncryption-utopian-witness-gif](https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif)](https://steemit.com/~witnesses)

**Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x**
properties (22)
authorutopian-io
permlinkre-andrixyz-how-to-use-ttrangeslider-to-filter-price-value-20180112t124203630z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-12 12:42:03
last_update2018-01-12 12:42:03
depth1
children0
last_payout2018-01-19 12:42:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,506
author_reputation152,955,367,999,756
root_title"How to use TTRangeSlider to filter price value in iOS development"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id28,994,816
net_rshares0