UIControl delegate Two Thumbs Slider

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

Aufgrund der Corona-Krise: Die Veröffentlichung von Stellenangeboten und -gesuchen ist bis 31.12.2020 kostenfrei. Das beinhaltet auch Angebote und Gesuche von und für Freischaffende und Selbstständige.

  • UIControl delegate Two Thumbs Slider

    Hallo Zusammen

    Ich bin an einem Slider mit zwei Thumbs. Das Ganze funktioniert nun ganz gut.
    Nun frage ich mich, wie ich dem UiControl für meinen custom Slider mitteilen kann, von wo er den Touch-Event aus meinem Tableview erhielt. Um den Range entsprechend anzupassen (siehe Anhang)
    Soweit ist mir klar das dies über ein delegate erfolgen muss... und ich vermute im - (void) touchesBegan:

    Kann mir da jemand auf die Sprünge helfen? ;)
  • sliderTwoTumbs.h

    PHP-Quellcode

    1. #import
    2. @interface sliderTwoTumbs : UIControl {
    3. IBOutlet UIImageView *LeftThumb;
    4. IBOutlet UIImageView *RightThumb;
    5. IBOutlet UIImageView *SliderBackground;
    6. IBOutlet UIImageView *SliderHighlight;
    7. IBOutlet UILabel *RightLabel;
    8. IBOutlet UILabel *LeftLabel;
    9. IBOutlet UIButton *BTNLeftThumb;
    10. CGPoint startLocation;
    11. CGFloat sliderConstant;
    12. CGRect circleRect;
    13. BOOL moving;
    14. id delegate;
    15. }
    16. @property(assign) CGFloat sliderConstant;
    17. @property (nonatomic, retain, readwrite) IBOutlet UIButton *BTNLeftThumb;
    18. @property (nonatomic, retain, readwrite) IBOutlet UILabel *LeftLabel;
    19. @property (nonatomic, retain, readwrite) IBOutlet UILabel *RightLabel;
    20. @property (nonatomic, retain, readwrite) IBOutlet UIImageView *LeftThumb;
    21. @property (nonatomic, retain, readwrite) IBOutlet UIImageView *RightThumb;
    22. @property (nonatomic, retain, readwrite) IBOutlet UIImageView *SliderBackground;
    23. @property (nonatomic, retain, readwrite) IBOutlet UIImageView *SliderHighlight;
    24. @property (assign) id delegate;
    25. -(void)setupUserInterface;
    26. -(void)sliderHighlight:(float)leftsidex rightsidex:(float)rightsidex;
    27. - (void)setThumbsSliderCount:(int)thumbsCount;
    28. @end
    Alles anzeigen

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von hst68 ()

  • sliderTwoTumbs.h



    Quellcode

    1. #import "sliderTwoTumbs.h"
    2. #define SLIDER_DISPLAY_WIDTH 284.0
    3. #define THUMB_WIDTH 34.0
    4. #define THUMB_HEIGHT 34.0
    5. #define BG_WIDTH 284.0
    6. #define BG_HEIGHT 9.0
    7. #define LABEL_HEIGHT 34.0
    8. #define LABEL_WIDTH 50.0
    9. #define LABEL_FONTSIZE 15.0
    10. #define LABEL_Y -(THUMB_CENTER_Y+THUMB_HEIGHT-17)
    11. #define THUMB_CENTER_Y (THUMB_HEIGHT / 2) + (BG_HEIGHT / 2)
    12. #define THUMB_RIGHT_X (BG_WIDTH - THUMB_WIDTH)+1
    13. #define SLIDER_FOR_BACKGROUND CGRectMake(0.0, 14.0, BG_WIDTH, BG_HEIGHT)
    14. #define SLIDER_FOR_HIGHLIGHT CGRectMake(0.0, 0.0, BG_WIDTH, BG_HEIGHT)
    15. #define THUMB_LEFT CGRectMake(0.0, -THUMB_CENTER_Y, THUMB_WIDTH, THUMB_HEIGHT)
    16. #define THUMB_RIGHT CGRectMake(THUMB_RIGHT_X, -THUMB_CENTER_Y, THUMB_WIDTH, THUMB_HEIGHT)
    17. #define LABEL_LEFT CGRectMake(0.0, LABEL_Y, LABEL_WIDTH, LABEL_HEIGHT)
    18. #define LABEL_RIGHT CGRectMake(THUMB_RIGHT_X, LABEL_Y, LABEL_WIDTH, LABEL_HEIGHT)
    19. @implementation sliderTwoTumbs
    20. @synthesize LeftThumb;
    21. @synthesize RightThumb;
    22. @synthesize SliderBackground;
    23. @synthesize SliderHighlight;
    24. @synthesize sliderConstant;
    25. @synthesize LeftLabel;
    26. @synthesize RightLabel;
    27. @synthesize BTNLeftThumb;
    28. @synthesize delegate;
    29. //int _hitCount=0;
    30. int _hitObject=0;
    31. float colladFromRight=THUMB_RIGHT_X - (THUMB_WIDTH / 2);
    32. float colladFromLeft=0 + (THUMB_WIDTH / 2);
    33. BOOL _on=NO;
    34. int _thumpsCount;
    35. - (void)drawRect:(CGRect)rect { // nothing}
    36. - (void)setThumbsSliderCount:(int)thumbsCount { _thumpsCount =thumbsCount;}
    37. - (void)setupUserInterface{
    38. self.backgroundColor = [UIColor clearColor];
    39. // Background image
    40. UIImageView* bg = [[UIImageView alloc] initWithFrame:SLIDER_FOR_BACKGROUND];
    41. bg.image = [UIImage imageNamed:@"sliderbackground.png"];
    42. bg.backgroundColor = [UIColor clearColor];
    43. bg.contentMode = UIViewContentModeLeft; self.SliderBackground =
    44. bg; [bg release];
    45. // Highlighted image
    46. UIImage *myImage = [UIImage imageNamed:@"sliderhighlight2.png"];
    47. CGRect cropRect = CGRectMake(2.0, 0.0, BG_WIDTH - 4, BG_HEIGHT);
    48. CGImageRef croppedImage = CGImageCreateWithImageInRect([myImage CGImage], cropRect);
    49. UIImageView *bgh = [[UIImageView alloc] initWithFrame:cropRect];
    50. [bgh setImage:[UIImage imageWithCGImage:croppedImage]];
    51. CGImageRelease(croppedImage); self.SliderHighlight = bgh;
    52. [bgh release];
    53. // thumbleft image
    54. UIImageView* thumb1 = [[UIImageView alloc] initWithFrame:THUMB_LEFT];
    55. thumb1.image = [UIImage imageNamed:@"leftthumb.png"];
    56. thumb1.backgroundColor = [UIColor clearColor];
    57. thumb1.contentMode = UIViewContentModeLeft;
    58. thumb1.userInteractionEnabled=YES;
    59. self.LeftThumb = thumb1;
    60. [thumb1 release];
    61. // Label left Range Value
    62. UILabel *lblleft =[[UILabel alloc]initWithFrame:LABEL_LEFT];
    63. lblleft.backgroundColor =[UIColor clearColor];
    64. LeftLabel.text=@"Label left";
    65. self.LeftLabel= lblleft;
    66. self.LeftLabel.font = [UIFont fontWithName:@"Arial" size:(LABEL_FONTSIZE)];
    67. [lblleft release];
    68. // thumbright image
    69. UIImageView* thumb2 = [[UIImageView alloc] initWithFrame:THUMB_RIGHT];
    70. thumb2.image = [UIImage imageNamed:@"rightthumb.png"];
    71. thumb2.backgroundColor = [UIColor clearColor];
    72. thumb2.contentMode = UIViewContentModeLeft;
    73. thumb2.userInteractionEnabled=YES;
    74. self.RightThumb = thumb2;
    75. [thumb2 release];
    76. // Label right Range Value
    77. UILabel *lblright =[[UILabel alloc]initWithFrame:LABEL_RIGHT];
    78. lblright.backgroundColor =[UIColor clearColor];
    79. self.RightLabel= lblright;
    80. self.RightLabel.font = [UIFont fontWithName:@"Arial" size:(LABEL_FONTSIZE)];
    81. [lblright release];
    82. [self addSubview:self.SliderBackground];
    83. [self.SliderBackground addSubview:self.SliderHighlight];
    84. [self.SliderBackground addSubview:self.LeftLabel];
    85. [self.SliderBackground addSubview:self.LeftThumb];
    86. [self.SliderBackground addSubview:self.RightLabel];
    87. [self.SliderBackground addSubview:self.RightThumb];
    88. }
    89. - (BOOL)isOn{
    90. return _on;
    91. }
    92. - (id)initWithFrame:(CGRect)frame {
    93. if (self = [super initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, SLIDER_DISPLAY_WIDTH, THUMB_HEIGHT+20)]) {
    94. self.backgroundColor = [UIColor clearColor];
    95. //self.clipsToBounds = YES;
    96. self.autoresizesSubviews = YES;
    97. self.autoresizingMask = 0;
    98. self.opaque = YES;
    99. [self setupUserInterface];
    100. }
    101. return self;
    102. }
    103. - (void)awakeFromNib{
    104. [self setupUserInterface];
    105. }
    106. - (void)setCircleCenter:(CGPoint)centerPoint{
    107. CGSize centerSize;
    108. centerSize.width=circleRect.size.width/2;
    109. centerSize.height=circleRect.size.width/2;
    110. circleRect.origin.x=centerPoint.x - centerSize.width;
    111. circleRect.origin.x=centerPoint.y - centerSize.height;
    112. }
    113. - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    114. UITouch *touch = [[event allTouches] anyObject];
    115. UITouch * touch2 =[touches anyObject];
    116. CGPoint location = [touch locationInView:touch.view];
    117. CGPoint touchPoint;
    118. if(CGRectContainsPoint(LeftThumb.frame, location)) {
    119. _on=YES;
    120. _hitObject=1;
    121. touchPoint =[touch2 locationInView:LeftThumb];
    122. }
    123. if(CGRectContainsPoint(RightThumb.frame, location)) {
    124. _on=YES;
    125. _hitObject=2;
    126. touchPoint =[touch2 locationInView:RightThumb];
    127. }
    128. if ([touches count]==1 && _on) {
    129. if (CGRectContainsPoint(circleRect, touchPoint)) {
    130. moving=YES;
    131. [self setCircleCenter:touchPoint];
    132. [self setNeedsDisplay];
    133. }
    134. moving=YES;
    135. [self setCircleCenter:touchPoint];
    136. [self setNeedsDisplay];
    137. }
    138. }
    139. - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    140. if (moving && [touches count]==1) {
    141. UITouch *touch =[touches anyObject];
    142. CGPoint loc;
    143. CGPoint prevloc;
    144. CGRect myFrame;
    145. CGPoint centerPoint;
    146. if (_hitObject==1) {
    147. centerPoint =[touch locationInView:LeftThumb];
    148. myFrame = LeftThumb.frame;
    149. }
    150. if (_hitObject==2) {
    151. centerPoint =[touch locationInView:RightThumb];
    152. myFrame = RightThumb.frame;
    153. }
    154. [self setCircleCenter:centerPoint];
    155. [self setNeedsDisplay];
    156. loc = [touch locationInView:self];
    157. prevloc = [touch previousLocationInView:self];
    158. float deltaX = loc.x - prevloc.x;
    159. myFrame.origin.x += deltaX;
    160. //NSLog(@"x Cordinated: %f",myFrame.origin.x);
    161. if (_hitObject==1 && myFrame.origin.x >=0 && myFrame.origin.x < colladFromRight) {
    162. [LeftThumb setFrame:myFrame];
    163. LeftThumb.image = [UIImage imageNamed:@"selectedthumb.png"];
    164. colladFromRight=(RightThumb.frame.origin.x - (THUMB_WIDTH-1)); }
    165. if (_hitObject==2 && myFrame.origin.x colladFromLeft) {
    166. [RightThumb setFrame:myFrame];
    167. RightThumb.image = [UIImage imageNamed:@"selectedthumb.png"];
    168. colladFromLeft=(LeftThumb.frame.origin.x + (THUMB_WIDTH-1));
    169. }
    170. [self sliderHighlight:colladFromLeft rightsidex:colladFromRight];
    171. [self setNeedsDisplay];
    172. }}
    173. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    174. if (moving && [touches count]==1) {
    175. UITouch *touch =[touches anyObject];
    176. CGPoint centerPoint;
    177. if (_hitObject==1) {
    178. centerPoint =[touch locationInView:LeftThumb];
    179. LeftThumb.image = [UIImage imageNamed:@"leftthumb.png"];
    180. if (LeftThumb.frame.origin.x <= 1) {
    181. LeftLabel.text=@"egal";
    182. }
    183. }
    184. if (_hitObject==2) {
    185. centerPoint =[touch locationInView:RightThumb];
    186. RightThumb.image = [UIImage imageNamed:@"rightthumb.png"];
    187. if (RightThumb.frame.origin.x >= (THUMB_RIGHT_X-4)) {
    188. RightLabel.text=@"egal";
    189. }
    190. }
    191. [self setCircleCenter:centerPoint];
    192. [self setNeedsDisplay];
    193. }
    194. moving=NO;
    195. _on=NO;
    196. _hitObject=0;
    197. }
    198. -(void)sliderHighlight:(float)leftsidex rightsidex:(float)rightsidex {
    199. float diffWidth=RightThumb.frame.origin.x-LeftThumb.frame.origin.x;
    200. NSString *string;
    201. if (_hitObject==1) {
    202. string = [[NSString alloc] initWithFormat:@"%f",LeftThumb.frame.origin.x];
    203. self.SliderHighlight.frame = CGRectMake(LeftThumb.frame.origin.x +4 , 0, diffWidth, BG_HEIGHT);
    204. self.LeftLabel.text=string;
    205. self.LeftLabel.frame = CGRectMake(LeftThumb.frame.origin.x , LABEL_Y, LABEL_WIDTH, LABEL_HEIGHT);
    206. }
    207. if (_hitObject==2) {
    208. string = [[NSString alloc] initWithFormat:@"%f",RightThumb.frame.origin.x];
    209. self.SliderHighlight.frame = CGRectMake(LeftThumb.frame.origin.x +4, 0, diffWidth, BG_HEIGHT);
    210. self.RightLabel.text=string;
    211. self.RightLabel.frame = CGRectMake(RightThumb.frame.origin.x , LABEL_Y, LABEL_WIDTH, LABEL_HEIGHT);
    212. }
    213. [string release];
    214. }
    215. - (void)setFormulaConstant:(CGFloat)constant{
    216. // alpha as in the alpha of the equation (not opacity alpha)
    217. sliderConstant = 0.0f;
    218. [self setupUserInterface];}
    219. - (void)dealloc {
    220. [super dealloc];
    221. [LeftThumb release];
    222. [RightThumb release];
    223. [LeftLabel release];
    224. [RightLabel release];
    225. [SliderBackground release];
    226. [SliderHighlight release];}
    227. @end
    Alles anzeigen

    Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von hst68 () aus folgendem Grund: code editiert

  • tableViewSearchController.m

    UIController wird hier aufgerufen:

    PHP-Quellcode

    1. // Customize the appearance of table view cells.
    2. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    3. static NSString *CellIdentifier = @"Cell";
    4. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    5. if (cell == nil) {
    6. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    7. }
    8. // Configure the cell...
    9. CGAffineTransform scaling = CGAffineTransformMakeScale(0.7, 0.7);
    10. switch (indexPath.row) {
    11. case 0:
    12. searchField = [[UITextField alloc]initWithFrame:CGRectMake(60, 12, 240, 25)];
    13. searchField.delegate =self;
    14. searchField.placeholder =@"Locate Me";
    15. searchField.textAlignment = UITextAlignmentLeft;
    16. searchField.borderStyle = UITextBorderStyleRoundedRect;
    17. searchField.clearButtonMode = UITextFieldViewModeWhileEditing;
    18. searchField.returnKeyType = UIReturnKeyDone;
    19. [cell addSubview:searchField];
    20. cell.textLabel.text=@"Ort";
    21. cell.selectionStyle =UITableViewCellSelectionStyleNone;
    22. break;
    23. case 1:
    24. [self delegateThumbsSlider:1];
    25. mySlider1 =[[sliderOneThumbs alloc]init];
    26. mySlider1.frame = CGRectMake(60 , 0, mySlider1.frame.size.width+10, 80);
    27. [mySlider1 setTransform:scaling];
    28. mySlider1.tag=1000;
    29. //mySlider1.delegate = self;
    30. [mySlider1 setDelegate:self];
    31. [mySlider1 addTarget:self action:@selector(segmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged];
    32. cell.contentMode=UIViewContentModeScaleAspectFill;
    33. [cell addSubview:mySlider1];
    34. cell.textLabel.text=@"Zimmer";
    35. cell.selectionStyle =UITableViewCellSelectionStyleNone;
    36. //[mySlider1 release];
    37. cell.textLabel.text=@"Umkreis";
    38. cell.selectionStyle =UITableViewCellSelectionStyleNone;
    39. break;
    40. case 2:
    41. objectLabel = [[UILabel alloc]initWithFrame:CGRectMake(135, 10, 150, 20)];
    42. objectLabel.backgroundColor = [UIColor clearColor];
    43. objectLabel.textColor =[UIColor lightGrayColor];
    44. objectLabel.textAlignment =UITextAlignmentRight;
    45. objectLabel.text=@"Haus";
    46. [cell addSubview:objectLabel];
    47. cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
    48. cell.textLabel.text=@"Objektart";
    49. break;
    50. case 3:
    51. [self delegateThumbsSlider:2];
    52. mySlider2 =[[sliderTwoTumbs alloc]init];
    53. mySlider2.frame = CGRectMake(60 , 0, mySlider2.frame.size.width+10, 80);
    54. [mySlider2 setTransform:scaling]; mySlider1.tag=1001;
    55. cell.contentMode=UIViewContentModeScaleAspectFill;
    56. [cell addSubview:mySlider2];
    57. cell.textLabel.text=@"Preis";
    58. cell.selectionStyle =UITableViewCellSelectionStyleNone;
    59. [mySlider2 release];
    60. break;
    61. case 4:
    62. [self delegateThumbsSlider:3];
    63. mySlider3 =[[sliderTwoTumbs alloc]init];
    64. mySlider3.frame = CGRectMake(60 , 0, mySlider3.frame.size.width+10, 80);
    65. [mySlider3 setTransform:scaling];
    66. mySlider1.tag=1002;
    67. cell.contentMode=UIViewContentModeScaleAspectFill;
    68. [cell addSubview:mySlider3];
    69. cell.textLabel.text=@"Zimmer";
    70. cell.selectionStyle =UITableViewCellSelectionStyleNone;
    71. [mySlider3 release];
    72. break;
    73. case 5:
    74. switchCriteria = [[UISwitch alloc]initWithFrame:CGRectMake(210, 10, 200, 20)];
    75. [switchCriteria addTarget:self action:@selector(sliderBTN:) forControlEvents:UIControlEventAllTouchEvents];
    76. [cell addSubview:switchCriteria];
    77. cell.textLabel.text=@"Erweiterte Kriterien";
    78. cell.selectionStyle =UITableViewCellSelectionStyleNone;
    79. break;
    80. default:
    81. break;
    82. }
    83. return cell;}
    Alles anzeigen

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von hst68 ()