UIButton is not working on didSelectAnnotationView - ios

Hi I have custom view in didSelectAnnotationView when user tap on the pin maker I'm displaying the custom view with UILabel and a UIButton everything is working fine but the UIButton is not clickable please tell how to resolve this issue.
My code.
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view1
{
NSString *bult;
if ([view1.annotation isKindOfClass:[MapPin class]]) {
MapPin *annot = view1.annotation;
NSLog(#"tit%#",annot.nsname);
bult=annot.nsname;
}
myview = [[UIView alloc]initWithFrame:CGRectMake(-60, -110, 200, 100)];
myview.layer.cornerRadius = 10;
myview.backgroundColor = [UIColor yellowColor];
myview.userInteractionEnabled = YES;
[view1 addSubview:test];
buldingname = [[UILabel alloc] initWithFrame:CGRectMake(5, -15, 150, 80)];
buldingname.text=bult;
[buldingname setTextColor:[UIColor blackColor]];
[buldingname setBackgroundColor:[UIColor clearColor]];
[buldingname setFont:[UIFont fontWithName: #"Trebuchet MS" size: 14.0f]];
buldingname.lineBreakMode = NSLineBreakByWordWrapping;
buldingname.numberOfLines = 3;
[myview addSubview:buldingname];
moredetail=[UIButton buttonWithType:UIButtonTypeRoundedRect];
moredetail.frame= CGRectMake(0, 55, 200, 50);
[moredetail setTitle:#"Click here for more details" forState:UIControlStateNormal];
[moredetail addTarget:self action:#selector(nextbtn:) forControlEvents:UIControlEventTouchUpInside];
[moredetail setExclusiveTouch:YES];
[myview addSubview:moredetail];
}
My calling method.
- (IBAction)nextbtn:(id)sender
{
NSLog(#"click");
}
I have used the above code its not working please tell me where I'm doing wrong how to resolve this issue.
Thanks in Advance.

It is not working because the mapView still handles the touch event.
Just add an DetailView in the:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
return nil;
}
static NSString *reuseIdentifier = #"Your_identifier";
MKAnnotationView *view = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdentifier];
if (view == nil)
{
view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
view.image = [UIImage imageNamed:#"your_annotaion_image.png"];
}
view.canShowCallout = YES;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
view.rightCalloutAccessoryView = rightButton;
UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"your_icon_image"]];
view.leftCalloutAccessoryView = icon;
return view;
}
and then you can call:
-(void)showDetails:(UIButton*)button
{
//do your stuff here
}
there is absolutely no need to build a annotation callout view yourself, just change the design of the default one.

You need to be add sub view to your custom view then its work.
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view1
{
NSString *bult;
if ([view1.annotation isKindOfClass:[MapPin class]]) {
MapPin *annot = view1.annotation;
NSLog(#"tit%#",annot.nsname);
bult=annot.nsname;
}
myview = [[UIView alloc]initWithFrame:CGRectMake(-60, -110, 200, 100)];
myview.layer.cornerRadius = 10;
myview.backgroundColor = [UIColor yellowColor];
myview.userInteractionEnabled = YES;
[view1 addSubview:myview];
buldingname = [[UILabel alloc] initWithFrame:CGRectMake(5, -15, 150, 80)];
buldingname.text=bult;
[buldingname setTextColor:[UIColor blackColor]];
[buldingname setBackgroundColor:[UIColor clearColor]];
[buldingname setFont:[UIFont fontWithName: #"Trebuchet MS" size: 14.0f]];
buldingname.lineBreakMode = NSLineBreakByWordWrapping;
buldingname.numberOfLines = 3;
[myview addSubview:buldingname];
moredetail=[UIButton buttonWithType:UIButtonTypeRoundedRect];
moredetail.frame= CGRectMake(0, 55, 200, 50);
[moredetail setTitle:#"Click here for more details" forState:UIControlStateNormal];
[moredetail addTarget:self action:#selector(nextbtn:) forControlEvents:UIControlEventTouchUpInside];
[moredetail setExclusiveTouch:YES];
[myview addSubview:moredetail];
}
check this code

Related

Make Google Maker like a Toggle Button

Actually i am adding iconview to marker and change to another iconview when one tap on that marker. this can be easily done by adding iconview in didTapMarker delegate method. But How to change to default view when one select to another Marker. just like Toggle Button
- (void)viewDidLoad
{
for(int i=0;i<lat.count;i++)
{
CLLocationCoordinate2D position = CLLocationCoordinate2DMake([[lat objectAtIndex:i]doubleValue],[[longit objectAtIndex:i]doubleValue]);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 69, 60)];
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 69, 21)];
label.text = #"Hello";
label.font = [UIFont systemFontOfSize:10];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor colorWithRed:24.0/255.0 green:59.0/255.0 blue:91.0/255.0 alpha:1.0];
label.backgroundColor = [UIColor colorWithRed:177.0/255.0 green:177.0/255.0 blue:177.0/255.0 alpha:1.0];
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 21, 69, 38)];
[btn setImage:[UIImage imageNamed:#"map2_"] forState:UIControlStateNormal];
[view addSubview:label];
[view addSubview:btn];
marker.iconView = view;
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.map = _mapView;
}
}
and in didTapMarker delegate method
-(BOOL) mapView:(GMSMapView *) mapView didTapMarker:(GMSMarker *)marker
{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 69, 60)];
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 69, 21)];
label.text = #"Hello";
label.font = [UIFont systemFontOfSize:10];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor colorWithRed:32.0/255.0 green:139.0/255.0 blue:58.0/255.0 alpha:1.0];
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 21, 69, 38)];
[btn setImage:[UIImage imageNamed:#"map3_"] forState:UIControlStateNormal];
[view addSubview:label];
[view addSubview:btn];
marker.iconView = view;
return YES;
}
You can userData property of GMSMarker for that, first set the userData with all your marker, so add this line inside your for loop of viewDidLoad where you are adding marker on Map.
marker.userData = #{#"isSelected":[NSNumber numberWithInt:0]};
Now on didTapMarker check like this
-(BOOL) mapView:(GMSMapView *) mapView didTapMarker:(GMSMarker *)marker
{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 69, 60)];
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 69, 21)];
label.text = #"Hello";
label.font = [UIFont systemFontOfSize:10];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor colorWithRed:32.0/255.0 green:139.0/255.0 blue:58.0/255.0 alpha:1.0];
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 21, 69, 38)];
NSNumber *number = [marker.userData objectForKey:#"isSelected"];
if ([number integerValue] == 0) {
[btn setImage:[UIImage imageNamed:#"map3_"] forState:UIControlStateNormal];
marker.userData = #{#"isSelected":[NSNumber numberWithInt:1]};
}
else {
[btn setImage:[UIImage imageNamed:#"map2_"] forState:UIControlStateNormal];
marker.userData = #{#"isSelected":[NSNumber numberWithInt:0]};
}
[view addSubview:label];
[view addSubview:btn];
marker.iconView = view;
return YES;
}

App crashes with received memory Exception in ARC while scrolling the scrollview with out using images

I am Using UIScrollview and adding subView to display the message. While scrolling it gives received memory exception. I am struggling to find this from 15 days. Please help me to solve this.
To display I am loading 5 messages first time. After scrolling adding the subView according to array count.If it is subView is increased according the message length.If it is long messages,It crashes with received memory exception.
This is my code
-(void)createView:(float)yAxis
{
// NSLog(#"Create view %f",yAxis);
for (UIView * view in backgroundScroll.subviews) {
if (view.tag == 15) {
[view removeFromSuperview];
}
}
for(int i=0;i<[pageMessageArray count];i++)
{
UIView *backgrounf_View=[self messageChildElements:yAxis :i];
[del.msglistViewArray addObject:backgrounf_View];
UIView *backgrounf_View1= [del.msglistViewArray objectAtIndex:i];
[backgroundScroll addSubview:backgrounf_View1];
yAxis=yAxis+oldY_Axis+65;
}
CGRect contentRect = CGRectZero;
for (UIView *view in backgroundScroll.subviews)
{
contentRect = CGRectUnion(contentRect, view.frame);
}
//UIView *backgrounf_View1= [del.msglistViewArray objectAtIndex:i];
backgroundScroll.contentSize = CGSizeMake(backgroundScroll.frame.size.width, yAxis-10);
lblPermenentmessage.frame=CGRectMake(30, contentRect.size.height+20,800, 50);
//LastMessageYaxis=contentRect.size.height+20;
lblPermenentmessage.text=#"Messages are displayed for 24-48 hours and automatically deleted permanently.";
}
-(UIView *)messageChildElements:(float)yAxis :(int)index
{
// NSLog(#"Create view %f",yAxis);
communityObject *objMessage=[del.messageArray objectAtIndex:index];
UIView *backgrounf_View=nil;
UITextView *txt_Message=nil;
UIButton *btnURL =nil;
UIButton *btnAttachment=nil;
UIButton *btnDate=nil;
UIButton *btnSendBy=nil;
UIButton *btnAttachmentName=nil;
UIButton *btnSendTo=nil;
NSString *labelText = objMessage.Message;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:8];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
CGFloat height=[self getNewsize:objMessage.Message :backgroundScroll.frame.size.width-30];
backgrounf_View=[[UIView alloc]initWithFrame:CGRectMake(26,yAxis,backgroundScroll.frame.size.width-30,height)];
if([objMessage.inorout isEqualToString:#"out"])
{
//ye
[backgrounf_View setBackgroundColor:[UIColor colorWithRed:251/255.0f green:247/255.0f blue:193/255.0f alpha:1.0f]];
[[backgrounf_View layer] setBorderWidth:1.0f];
[[backgrounf_View layer] setBorderColor:[UIColor colorWithRed:236/255.0f green:237/255.0f blue:178/255.0f alpha:1.0f].CGColor];
}
else
{
//[cell.lblName addTarget:self action:#selector(replayClick:) forControlEvents:UIControlEventTouchUpInside];
[backgrounf_View setBackgroundColor:[UIColor colorWithRed:251/255.0f green:215/255.0f blue:245/255.0f alpha:1.0f]];
[[backgrounf_View layer] setBorderWidth:1.0f];
[[backgrounf_View layer] setBorderColor:[UIColor colorWithRed:226/255.0f green:188/255.0f blue:218/255.0f alpha:1.0f].CGColor];
}
// backgrounf_View.autoresizingMask=UIViewAutoresizingFlexibleWidth;
backgrounf_View.layer.cornerRadius=10;
//backgrounf_View.tag=15;
backgrounf_View.clipsToBounds=YES;
backgrounf_View.tag=objMessage.indexValue;
txt_Message = [[UITextView alloc] initWithFrame:CGRectMake(12, 6, backgroundScroll.frame.size.width-40, height)];
txt_Message.backgroundColor=[UIColor redColor];
[txt_Message setScrollEnabled:NO];
[txt_Message setUserInteractionEnabled:NO];
[txt_Message setBackgroundColor:[UIColor clearColor]];
//txt_Message.autoresizingMask=UIViewAutoresizingFlexibleWidth;
txt_Message.attributedText=attributedString;
//[txt_Message sizeToFit];
if([del.device isEqualToString:#"iphone"])
{
[txt_Message setFont:[UIFont fontWithName:#"Arial" size:17]];
}
else
{
[txt_Message setFont:[UIFont fontWithName:#"Arial" size:21]];
}
[backgrounf_View addSubview:txt_Message];
oldY_Axis=height+20;
if (objMessage.Link.length>0)
{
btnURL = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnURL.frame = CGRectMake(11,height,backgroundScroll.frame.size.width-100,40);
[btnURL setTitle:objMessage.Link forState:UIControlStateNormal];
btnURL.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btnURL.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
// btnURL.backgroundColor=[UIColor redColor];
btnURL.tag=objMessage.indexValue;
btnURL.titleLabel.lineBreakMode=UILineBreakModeCharacterWrap;
btnURL.titleLabel.numberOfLines=2;
btnURL.titleLabel.font=[UIFont fontWithName:#"Arial" size:20];
btnURL.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[btnURL setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[backgrounf_View addSubview:btnURL];
oldY_Axis=height+btnURL.frame.size.height+20;
}
if (objMessage.Filename.length>0)
{
btnAttachment= [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnAttachment.frame = CGRectMake(11, oldY_Axis,40, 40);
btnAttachment.tag=objMessage.indexValue;
UIImage *buttonattachment=[UIImage imageNamed:#"attach.png"];
[btnAttachment setBackgroundImage:buttonattachment forState:UIControlStateNormal];
btnAttachment.userInteractionEnabled=YES;
btnAttachmentName = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnAttachmentName.frame = CGRectMake(47,oldY_Axis+5,backgroundScroll.frame.size.width-150,60);
[btnAttachmentName setTitle:objMessage.Filename forState:UIControlStateNormal];
btnAttachmentName.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btnAttachmentName.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
btnAttachmentName.tag=objMessage.indexValue;
btnAttachmentName.titleLabel.font=[UIFont fontWithName:#"Arial" size:20];
btnAttachmentName.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[btnAttachmentName setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[backgrounf_View addSubview:btnAttachment];
[backgrounf_View addSubview:btnAttachmentName];
oldY_Axis=oldY_Axis+btnAttachment.frame.size.height+30;
}
btnDate = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnDate.frame = CGRectMake(11,oldY_Axis,130,30);
[btnDate setTitle:[NSString stringWithFormat:#"%#:",objMessage.Date] forState:UIControlStateNormal];
btnDate.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btnDate.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
btnDate.tag=objMessage.indexValue;
btnDate.titleLabel.font=[UIFont fontWithName:#"Arial" size:15];
btnDate.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[btnDate setTitleColor:[UIColor colorWithRed:70/255.0f green:70/255.0f blue:70/255.0f alpha:1.0f] forState:UIControlStateNormal];
[backgrounf_View addSubview:btnDate];
btnSendBy = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnSendBy.frame = CGRectMake(127,oldY_Axis,backgroundScroll.frame.size.width-50,200);
[btnSendBy setTitle:objMessage.senderPerson forState:UIControlStateNormal];
btnSendBy.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btnSendBy.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
btnSendBy.tag=objMessage.indexValue;
// btnURL.backgroundColor=[UIColor redColor];
//btnSendBy.titleLabel.lineBreakMode=UILineBreakModeCharacterWrap;
btnSendBy.titleLabel.font=[UIFont fontWithName:#"Arial" size:15];
btnSendBy.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[btnSendBy setTitleColor:[UIColor colorWithRed:70/255.0f green:70/255.0f blue:70/255.0f alpha:1.0f] forState:UIControlStateNormal];
[backgrounf_View addSubview:btnSendBy];
btnSendTo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnSendTo.frame = CGRectMake(11,oldY_Axis+25,backgroundScroll.frame.size.width-50,200);
[btnSendTo setTitle:objMessage.senderPerson forState:UIControlStateNormal];
btnSendTo.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btnSendTo.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
// btnURL.backgroundColor=[UIColor redColor];
//btnSendBy.titleLabel.lineBreakMode=UILineBreakModeCharacterWrap;
btnSendTo.titleLabel.font=[UIFont fontWithName:#"Arial" size:15];
btnSendTo.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[btnSendTo setTitleColor:[UIColor colorWithRed:70/255.0f green:70/255.0f blue:70/255.0f alpha:1.0f] forState:UIControlStateNormal];
[backgrounf_View addSubview:btnSendTo];
NSString *communityName=#"";
if([del.communityId isEqualToString:#"none"])
{
communityName=objMessage.Community;
communityName=[NSString stringWithFormat:#"%#: ",objMessage.Community];
}
//NSLog(#"Group type %# and %#",objMessage.groupType,del.communityId);
if([objMessage.groupType isEqualToString:#"Personal"])
{
[btnSendTo setTitle:[NSString stringWithFormat:#"%#%#",communityName,objMessage.Person] forState: UIControlStateNormal];
}
else if([objMessage.groupType isEqualToString:#"Everyone"])
{
[btnSendTo setTitle:[NSString stringWithFormat:#"%#%#",communityName,#"Everyone"] forState: UIControlStateNormal];
}
else
{
[btnSendTo setTitle:[NSString stringWithFormat:#"%#%#",communityName,objMessage.GroupName] forState: UIControlStateNormal];
}
[btnURL addTarget:self action:#selector(getLink:) forControlEvents:UIControlEventTouchUpInside];
[btnAttachmentName addTarget:self action:#selector(fileDowload:) forControlEvents:UIControlEventTouchUpInside];
[btnAttachment addTarget:self action:#selector(fileDowload:) forControlEvents:UIControlEventTouchUpInside];
backgrounf_View.frame=CGRectMake(24,yAxis,backgroundScroll.frame.size.width-30,oldY_Axis+55);
LastMessageYaxis=backgrounf_View.frame.origin.y+backgrounf_View.frame.size.height+10;
del.RefreshValue=0;
LastValue=index;
communityObject *com=[[communityObject alloc]init];
com.MessageId=nil;
com.Date=nil;
com.Message=nil;
com.Link=nil;
com.Attachment=nil;
com.Filename=nil;
com.attachmentType=nil;
com.imageSize=nil;
com.groupType=nil;
com.Person=nil;
com.GroupName=nil;
com.senderPerson=nil;
com.Community=nil;
com.inorout=nil;
return backgrounf_View;
}
Sceenshot is added please check.
This bit looks like a mutation of array while enumerating it:
for (UIView * view in backgroundScroll.subviews) {
if (view.tag == 15) {
[view removeFromSuperview];
}
}
There is no wonder why the array doesn't like it. I would crash too :)
BTW: That part where it is storing indexes of array in view's tag seems very wrong.

Can't touch UIButton in UIScrollView

I am programmatically creating buttons in a view inside UIScrollView. I also have programmatically created UITextField. I can select and enter data into the text fields all day long. But I cannot get any sign the buttons are being touched. I can add them to the main view and they work. But when added to the view inside the UIScrollView, they are lost in another dimension. I've read plenty of info about setting userInteraction, delayContentTouches, and intercepting via gestureRecognizer. The last might be related to my problem. I return NO via [touch.view isDescendantOfView:self.myScrollViewName]. I can't get it to trigger via [touch.view isKindOfClass:[UIButton class]]. Which makes me think I have a more fundamental error I am missing?
related:
https://stackoverflow.com/a/6825839/4050489
edited to add button code per request:
self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.myButton.frame = myButtonFrame;
self.myButton.enabled = interface.isActive;
self.myButton.userInteractionEnabled = YES;
self.myButton.exclusiveTouch = YES;
self.myButton.backgroundColor = [UIColor greenColor];
//self.myButton. = YES;
self.myButton.layer.borderWidth=1.0f;
self.myButton.layer.borderColor=[[UIColor blackColor] CGColor];
self.myButton.layer.cornerRadius = 10;
[self.myButton setTag:interface.tag];
[self.myButton setTitle:interface.Name forState:UIControlStateNormal];
[self.myButton addTarget:self action:#selector(navigatePartButtons:) forControlEvents:UIControlEventTouchUpInside];
[self.myButton setEnabled:YES]; //error check
[self.partSetupView addSubview:self.myButton];
partSetupView is a UIView in the UIScrollView.
Try this sample Code:
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *myScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, 300, 300)];
myScroll.backgroundColor = [UIColor redColor];
[self.view addSubview:myScroll];
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 200)];
myView.backgroundColor = [UIColor yellowColor];
[myScroll addSubview:myView];
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 300, 100)];
myButton.backgroundColor = [UIColor blackColor];
[myView addSubview:myButton];
[myButton addTarget:self action:#selector(btnClick) forControlEvents:UIControlEventTouchDown];
}
-(void)btnClick{
NSLog(#"Button Click");
}
#end

IOS UITableview Scroll is not smooth in IOS6

And I complete IOS app in IOS 7 and its works fine IOS 7 and then I start converting my app to IOS 6 I face lot of problems.After a huge struggle I rectify almost all the issuse other that Performance issue
I am using UITableview to display all the contents and when I test my app in Iphone 5c there is no problem in scrolling the table view. And the I test my app in ipod retina list is not scrolling smoothly. It is one of the huge problem for me and also Its killing my time
To illustrate my issue I have added my tableview cell code below. Please Suggest me some quick solution
UITableviewCell Code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellidentifier=#"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellidentifier];
if (cell ==nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier]autorelease];
}
[[cell.contentView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
cell.selectionStyle = UITableViewCellEditingStyleNone;
UIView * contentview = [[[UIView alloc]init]autorelease];
UIImageView * userimage = [[[UIImageView alloc]init]autorelease];
UIImageView * itemimageview = [[[UIImageView alloc]init]autorelease];
UIView * bottomview = [[[UIView alloc]init]autorelease];
UILabel * imagenameLable = [[[UILabel alloc]init]autorelease];
UILabel * usernameLable = [[[UILabel alloc]init]autorelease];
UILabel * itemcostLable = [[[UILabel alloc]init]autorelease];
UIButton*fancybtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton * addrditbtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton * commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UILabel * noofcommentsLable = [[[UILabel alloc]init]autorelease];
// [contentBgImageview setImage:[UIImage imageNamed:#"item_bg.png"]];
[itemimageview setAutoresizesSubviews:YES];
// [itemimageview setContentMode:UIViewContentModeScaleAspectFill];
[itemimageview setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",[[[[homePageArray objectAtIndex:indexPath.row]objectForKey:#"photos"]objectAtIndex:0]objectForKey:#"item_url_main_350"]]]];
[itemimageview setContentMode:UIViewContentModeScaleAspectFit];
[itemimageview setTag:indexPath.row];
[userimage setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",[[[[homePageArray objectAtIndex:indexPath.row]objectForKey:#"photos"]objectAtIndex:0]objectForKey:#"user_url_main_70"]]]];
[bottomview setBackgroundColor:[UIColor colorWithRed:200/255 green:54/255 blue:54/255 alpha:0.4]];
[bottomview setBackgroundColor:[UIColor colorWithRed:255 green:255 blue:255 alpha:1]];
// [bottomview setAlpha:0.5];
[imagenameLable setText:[[homePageArray objectAtIndex:indexPath.row]objectForKey:#"item_title"]];
[imagenameLable setTextColor:[UIColor blackColor]];
[imagenameLable setFont:[UIFont fontWithName:#"Helvetica-Bold" size:14]];
[imagenameLable setBackgroundColor:[UIColor clearColor]];
[bottomview addSubview:imagenameLable];
[usernameLable setText:[NSString stringWithFormat:#"#%#",[[homePageArray objectAtIndex:indexPath.row]objectForKey:#"sellername"]]];
[usernameLable setTextColor:[UIColor grayColor]];
[usernameLable setFont:[UIFont fontWithName:#"Helvetica" size:10]];
[usernameLable setBackgroundColor:[UIColor clearColor]];
[bottomview addSubview:usernameLable];
[itemcostLable setText:[NSString stringWithFormat:#"%# %#",delegate.currencyStr,[[homePageArray objectAtIndex:indexPath.row]objectForKey:#"price"]]];
[itemcostLable setTextColor:[UIColor grayColor]];
[itemcostLable setFont:[UIFont fontWithName:#"Helvetica-Bold" size:16]];
[itemcostLable setBackgroundColor:[UIColor clearColor]];
[bottomview addSubview:itemcostLable];
// [addrditbtn setImage:[UIImage imageNamed:#"add.png"] forState:UIControlStateNormal];
if ([[[homePageArray objectAtIndex:indexPath.row]objectForKey:#"liked"]isEqualToString:#"No"])
{
[fancybtn setImage:[UIImage imageNamed:#"fantacybtn.png"] forState:UIControlStateNormal];
[addrditbtn setImage:[UIImage imageNamed:#"addtolist.png"] forState:UIControlStateNormal];
}
else
{
[fancybtn setImage:[UIImage imageNamed:#"fantacydbtn.png"] forState:UIControlStateNormal];
[addrditbtn setImage:[UIImage imageNamed:#"addtolist.png"] forState:UIControlStateNormal];
}
NSString*itemid=[[homePageArray objectAtIndex:indexPath.row]objectForKey:#"id"];
if ([delegate.fantacyitemsArray containsObject:itemid]) {
[fancybtn setImage:[UIImage imageNamed:#"fantacydbtn.png"] forState:UIControlStateNormal];
[addrditbtn setImage:[UIImage imageNamed:#"addtolist.png"] forState:UIControlStateNormal];
}
if ([delegate.unfantacyitemsArray containsObject:itemid]) {
[fancybtn setImage:[UIImage imageNamed:#"fantacybtn.png"] forState:UIControlStateNormal];
[addrditbtn setImage:[UIImage imageNamed:#"addtolist.png"] forState:UIControlStateNormal];
}
fancybtn.tag=[itemid intValue];
[fancybtn addTarget:self action:#selector(fancyBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
addrditbtn.tag=[itemid intValue];
[addrditbtn addTarget:self action:#selector(addtolistBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
[commentBtn setImage:[UIImage imageNamed:#"commentnew.png"] forState:UIControlStateNormal];
[commentBtn setUserInteractionEnabled:YES];
commentBtn.tag=indexPath.row;
[commentBtn addTarget:self action:#selector(commentBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
UIButton * usernameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[usernameBtn setUserInteractionEnabled:YES];
usernameBtn.tag=indexPath.row;
[usernameBtn addTarget:self action:#selector(usernameBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
[noofcommentsLable setTextColor:[UIColor grayColor]];
[noofcommentsLable setFont:[UIFont fontWithName:#"Helvetica" size:12]];
int commentcount = 0;
if([delegate.newaddedcommentDict objectForKey:[NSString stringWithFormat:#"%#Array",[[homePageArray objectAtIndex:indexPath.row]objectForKey:#"id"]]])
{
commentcount = [[[homePageArray objectAtIndex:indexPath.row]objectForKey:#"commentcount"]intValue]+[[delegate.newaddedcommentDict objectForKey:[NSString stringWithFormat:#"%#Array",[[homePageArray objectAtIndex:indexPath.row]objectForKey:#"id"]]]count];
}
else
{
commentcount = [[[homePageArray objectAtIndex:indexPath.row]objectForKey:#"commentcount"]intValue];
}
[noofcommentsLable setText:[NSString stringWithFormat:#"%d",commentcount]];
UIScrollView * scroll = [[[UIScrollView alloc]init]autorelease];
// [scroll setScrollEnabled:NO];
[scroll setUserInteractionEnabled:YES];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(gestureAction:)];
[recognizer setNumberOfTapsRequired:1];
scroll.userInteractionEnabled = YES;
[scroll addGestureRecognizer:recognizer];
[itemcostLable setTextAlignment:NSTextAlignmentRight];
[bottomview setFrame:CGRectMake(0,3,300,37)];
[itemimageview setFrame:CGRectMake(0,0,300,240)];
CGSize size;
if ([[[[homePageArray objectAtIndex:indexPath.row]objectForKey:#"photos"]objectAtIndex:0]objectForKey:#"width"]!=[NSNull null]||[[[[homePageArray objectAtIndex:indexPath.row]objectForKey:#"photos"]objectAtIndex:0]objectForKey:#"height"]!=[NSNull null])
{
size= [self aspectScaledImageSizeForImageView:itemimageview width:[[[[[homePageArray objectAtIndex:indexPath.row]objectForKey:#"photos"]objectAtIndex:0]objectForKey:#"width"]floatValue] height:[[[[[homePageArray objectAtIndex:indexPath.row]objectForKey:#"photos"]objectAtIndex:0]objectForKey:#"height"]floatValue]];
}
else
{
size = CGSizeMake(100,100);
}
[itemimageview setFrame:CGRectMake((300-size.width)/2,0,size.width,size.height)];
[scroll setFrame:CGRectMake(0,43,300,size.height)];
//place holder image
UIImageView * placeholderimageview = [[[UIImageView alloc]init]autorelease];
[placeholderimageview setFrame:CGRectMake(130,0,40,size.height)];
[placeholderimageview setImage:[UIImage imageNamed:#"57.png"]];
[placeholderimageview setContentMode:UIViewContentModeScaleAspectFit];
// [itemimageview setBackgroundColor:[UIColor redColor]];
[imagenameLable setFrame:CGRectMake(40,5,160,15)];
[usernameLable setFrame:CGRectMake(40,20,160,15)];
[usernameBtn setFrame:CGRectMake(0,5,200,30)];
[itemcostLable setFrame:CGRectMake(220,0,70,40)];
[fancybtn setFrame:CGRectMake(5,size.height+48,78,25)];
[commentBtn setFrame:CGRectMake(221,size.height+48,40,25)];
[noofcommentsLable setFrame:CGRectMake(240,size.height+48,20,25)];
[addrditbtn setFrame:CGRectMake(265,size.height+48,27,25)];
// [commentBtn setFrame:CGRectMake(191,size.height+48,40,25)];
// [noofcommentsLable setFrame:CGRectMake(211,size.height+48,20,25)];
// [addrditbtn setFrame:CGRectMake(236,size.height+48,27,25)];
// [cartbtn setFrame:CGRectMake(268,size.height+48,27,25)];
[contentview setFrame:CGRectMake(10,5,300,size.height+78)];
[userimage setFrame:CGRectMake(5,5,30,30)];
[scroll setBackgroundColor:[UIColor colorWithRed:0.976 green:0.976 blue:0.976 alpha:1]];
[scroll setBackgroundColor:[UIColor clearColor]];
contentview.clipsToBounds = NO;
contentview.layer.masksToBounds = NO;
contentview.layer.shadowColor = [[UIColor grayColor] CGColor];
contentview.layer.shadowOffset = CGSizeMake(0,1);
contentview.layer.shadowOpacity = 0.2;
contentview.layer.shadowRadius = 0.6;
contentview.layer.cornerRadius = 6.0; // set as you want.
[bottomview setBackgroundColor:[UIColor clearColor]];
userimage.layer.cornerRadius = 15.0;
userimage.layer.masksToBounds = YES;
[itemimageview setUserInteractionEnabled:YES];
[scroll setBackgroundColor:[UIColor clearColor]];
[bottomview setBackgroundColor:[UIColor clearColor]];
[contentview setBackgroundColor:[UIColor whiteColor]];
//place holder image
[contentview addSubview:scroll];
[scroll addSubview:placeholderimageview];
[scroll addSubview:itemimageview];
[contentview addSubview:bottomview];
[contentview addSubview:fancybtn];
[contentview addSubview:addrditbtn];
[contentview addSubview:commentBtn];
[contentview addSubview:noofcommentsLable];
[contentview addSubview:userimage];
[contentview addSubview:usernameLable];
[contentview addSubview:usernameBtn];
[cell.contentView addSubview:contentview];
[cell setBackgroundColor:[UIColor clearColor]];
[cell.contentView setBackgroundColor:[UIColor clearColor]];
// Configure the cell...
return cell;
}
Its because in every time when cellForRowAtIndexPath: method called you recreating all subviews again.
You must do it only once.
Make a class that inherits from UITableViewCell.
//MyCell.h file
#interface MyCell : UITableViewCell
//here declare all views that you need, e.g.
#property (strong, nonatomic) UILabel *imagenameLable;
#property (strong, nonatomic) UILabel *usernameLable;
// and so on. But only those, that you mast set value (or read values) outside of this class.
// the other views declare in MyClass.m file, so they are for private use (eg. contentview,etc...)
#end
//MyCell.m file
#interface MyCell()
// here declare private properties
#property (strong, nonatomic) UIView* contentview;
// and so on...
#end
#implementation
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self setupUI];
}
return self;
}
- (void)setupUI
{
//and finally, in this method build your views, i.e. allocate your views, add subview of this cell, set frames, font to labels, text colors, etc...
//e.g.
self.contentview = [[[UIView alloc]init]autorelease];
self.contentview.clipsToBounds = NO;
self.contentview.layer.masksToBounds = NO;
self.contentview.layer.shadowColor = [[UIColor grayColor] CGColor];
self.contentview.layer.shadowOffset = CGSizeMake(0,1);
self.contentview.layer.shadowOpacity = 0.2;
self.contentview.layer.shadowRadius = 0.6;
self.contentview.layer.cornerRadius = 6.0; // set as you want.
// initialize other views
[self.contentview addSubview:self.scroll];
[self.scroll addSubview:self.placeholderimageview];
[self.scroll addSubview:self.itemimageview];
[self.contentview addSubview:self.bottomview];
[self.contentview addSubview:self.fancybtn];
[self.contentview addSubview:self.addrditbtn];
[self.contentview addSubview:self.commentBtn];
[self.contentview addSubview:self.noofcommentsLable];
[self.contentview addSubview:self.userimage];
[self.contentview addSubview:self.usernameLable];
[self.contentview addSubview:self.usernameBtn];
[self.contentView addSubview:self.contentview];
// something like this.
}
#end
And finally
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellidentifier=#"cell";
MyCell *cell=[tableView dequeueReusableCellWithIdentifier:cellidentifier];
if (cell ==nil)
{
cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier]autorelease];
}
// here set values to properties of your cell. EG set text, change color if needed, change frame if needed, etc... But not remove and add them again!
cell.usernameLable.text = [NSString stringWithFormat:#"#%#",[[homePageArray objectAtIndex:indexPath.row]objectForKey:#"sellername"]]];
//etc...
return cell;
}
I found the solution for my code
This is because of
contentview.layer.shadowOffset = CGSizeMake(0,1);
contentview.layer.shadowOpacity = 0.2;
contentview.layer.shadowRadius = 0.6;
contentview.layer.cornerRadius = 6.0; // set as
Once I remove this line from my code Its works fine
There is lot of optimalization issues in your code. You should use Instruments with Time Profiler template to detect bottlenecks.
BTW, When you draw shadow of CALayer you should set CALayer's shadowPath property for outline of the shadow. This will speed up drawing your shadow much. Please refer documentation for more details.
dialogContainer.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:dialogContainer.bounds cornerRadius:dialogContainer.layer.cornerRadius].CGPath;

iOS iCarousel image offset

Good afternoon. ICarusel I use in my application. I have an image that is loaded as image view. Images are placed exactly in the center. Please tell me how do I move the image up to only 50 pixels in it without moving the rest of the content?
The method for creating views like that:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
NSString *docDir = [NSHomeDirectory() stringByAppendingPathComponent:#"Library/Caches"];
NSDictionary *myDic =[magazinesInfo objectAtIndex:index];
//Resize image
UIImage *img = [UIImage imageWithImage:[UIImage imageWithContentsOfFile:[NSString stringWithFormat:#"%#/%#_img.png",docDir,[myDic objectForKey:#"title"]]] scaledToSize:CGSizeMake(375,510)];
UIImageView *romb = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 118, 135)];
UIImageView *faceImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,768,1004)];
UIImage *dwImage = [UIImage imageNamed:#"button.png"];
UIImage *readImage = [UIImage imageNamed:#"read_button.png"];
UIImage *deleteImage = [UIImage imageNamed:#"delete_button.png"];
if(view ==nil)
{
UIButton *myDownloadButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[myDownloadButton setBackgroundColor:[UIColor blackColor]];
[myDownloadButton setHidden:YES];
romb.image = [UIImage imageNamed:#"romb.png"];
view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 768, 1004)];
[view setBackgroundColor:[UIColor blackColor]];
view = faceImage;
faceImage.image = nil;
((UIImageView *)view).image = nil;
view.contentMode = UIViewContentModeCenter;
//Magazine number
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(345, 85, 75, 29)];
myLabel.backgroundColor = [UIColor clearColor];
myLabel.textAlignment = NSTextAlignmentCenter;
[myLabel setFont:[UIFont fontWithName:#"OpenSans-Light" size:36.0f]];
myLabel.textColor = [UIColor whiteColor];
myLabel.tag = 1;
//Magazine name
nameMag = [[UILabel alloc] initWithFrame:CGRectMake(-65, 450, 100, 100)];
nameMag.backgroundColor = [UIColor yellowColor];
nameMag.textAlignment = NSTextAlignmentCenter;
[nameMag setFont:[UIFont fontWithName:#"OpenSans-Light" size:30.0f]];
nameMag.textColor = [UIColor blackColor];
nameMag.tag = 3;
//Date
dateMag = [[UILabel alloc] initWithFrame:CGRectMake(-67, 500, 500, 30)];
dateMag.backgroundColor = [UIColor greenColor];
dateMag.textAlignment = NSTextAlignmentCenter;
[dateMag setFont:[UIFont fontWithName:#"OpenSans-Light" size:20.0f]];
dateMag.textColor = [UIColor blackColor];
dateMag.tag = 4;
//Download button
downloadButton = [[UIButton alloc] initWithFrame:CGRectMake(350, 700, 128, 37)];
[downloadButton setBackgroundImage:dwImage forState:UIControlStateNormal];
[downloadButton addTarget:self action:#selector(pressDownload:) forControlEvents:UIControlEventTouchUpInside];
downloadButton.tag = 5;
//Read button
readButton = [[UIButton alloc] initWithFrame:CGRectMake(250, 750, 128, 37)];
[readButton setBackgroundImage:readImage forState:UIControlStateNormal];
[readButton addTarget:self action:#selector(readMag:) forControlEvents:UIControlEventTouchUpInside];
readButton.hidden=YES;
readButton.tag = 8;
//Delete button
deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(400, 750, 128, 37)];
[deleteButton setBackgroundImage:deleteImage forState:UIControlStateNormal];
[deleteButton addTarget:self action:#selector(deleteMag:) forControlEvents:UIControlEventTouchUpInside];
deleteButton.hidden=YES;
deleteButton.tag = 9;
//Progress bar
downloadProgress = [[UIProgressView alloc] initWithFrame:CGRectMake(100, 800, 127, 8)];
downloadPrecent = [[UILabel alloc]initWithFrame:CGRectMake(300, 800, 8, 8)];
downloadPrecent.backgroundColor = [UIColor blueColor];
[downloadPrecent setFont:[UIFont fontWithName:#"OpenSans-Light" size:20.0f]];
dateMag.textColor = [UIColor blackColor];
downloadProgress.tag = 6;
downloadPrecent.tag = 7;
downloadProgress.hidden = YES;
downloadPrecent.hidden = YES;
//Add image subview
[view addSubview:romb];
//Label subview
[view addSubview:myLabel];
[view addSubview:nameMag];
[view addSubview:dateMag];
//Progressbar subview
[view addSubview:downloadProgress];
[view addSubview:downloadPrecent];
//Buttons subview
[view addSubview:downloadButton];
[view addSubview:readButton];
[view addSubview:deleteButton];
[view addSubview:myDownloadButton];
}
else
{
romb.image = (UIImage*)[romb viewWithTag:3];
((UIImageView *)faceImage).image = (UIImage*)[view viewWithTag:2];
myLabel = (UILabel *)[view viewWithTag:1];
nameMag = (UILabel *)[view viewWithTag:3];
dateMag = (UILabel *)[view viewWithTag:4];
downloadProgress = (UIProgressView *) [view viewWithTag:6];
downloadPrecent = (UILabel *)[view viewWithTag:7];
downloadButton = (UIButton *) [view viewWithTag:5];
readButton = (UIButton *) [view viewWithTag:8];
deleteButton = (UIButton*) [view viewWithTag:9];
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSLog(#"index is: %i",index);
NSLog(#"current item index %i",[self.carousel currentItemIndex]);
NSLog(#"%hhd",[fileManager fileExistsAtPath:[NSString stringWithFormat:#"%#/%#_mag.pdf",docDir,[myDic objectForKey:#"title"]]]);
if ([fileManager fileExistsAtPath:[NSString stringWithFormat:#"%#/%#_mag.pdf",docDir,[myDic objectForKey:#"title"]]] == YES && (index == [self.carousel currentItemIndex]))
{
NSLog(#"%#",[[view subviews] objectAtIndex:9]);
[[[view subviews] objectAtIndex:9] setHidden:NO];
readButton.hidden = NO;
deleteButton.hidden = NO;
downloadButton.hidden = YES;
}
else
{
[[[view subviews] objectAtIndex:9] setHidden:YES];
readButton.hidden = YES;
deleteButton.hidden = YES;
downloadButton.hidden = NO;
}
((UIImageView *)view).image = img;
myLabel.text = [myDic objectForKey:#"title"];
dateMag.text = [myDic objectForKey:#"date"];
romb.image = [UIImage imageNamed:#"romb.png"];
return view;
}
Now it looks like this (do not worry, it's still developing :))
http://files.mail.ru/B2B41BA915624173B646184D4283D9F9?t=1
Use the contentOffset property to shift the carousel contents relative to the carousel view without changing their perspective.
You can use the viewpointOffset propert instead if you want it to look like the perspective has changed (i.e as if you are looking at the carousel from below).

Resources